<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.1.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"liukairui.me","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.10.1","exturl":false,"sidebar":{"position":"left","display":"hide","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":true,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null,"activeClass":"valine"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="介绍了Linux基础内容,主讲:尚硅谷李明与沈超,视频来自B站:BV1ut411a7ro">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux系统管理教程笔记">
<meta property="og:url" content="http://liukairui.me/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/">
<meta property="og:site_name" content="LiuKairui Website">
<meta property="og:description" content="介绍了Linux基础内容,主讲:尚硅谷李明与沈超,视频来自B站:BV1ut411a7ro">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-03-03T10:06:00.000Z">
<meta property="article:modified_time" content="2021-10-18T06:44:13.591Z">
<meta property="article:author" content="Liu Kairui">
<meta property="article:tag" content="运维">
<meta property="article:tag" content="Linux">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://liukairui.me/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://liukairui.me/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/","path":"article/Linux系统管理教程笔记/","title":"Linux系统管理教程笔记"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Linux系统管理教程笔记 | LiuKairui Website</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?ea07bceb8f5fa721da2ebf01833faa32"></script>



<div class="nindexBK">
<div class="nindexBKC"></div>
<div class="nindex">
  <h1>Hey👋</h1>
  <p style="font-weight: 500; font-size: 2.2em">欢迎访问Liu Kairui的个人站.</p>
  <p>
      我是一个来自重庆的在校大学生, 偏爱于前端技术<!--与OS设计-->, 偶尔使用 JS / Go 进行轻量项目开发, 欢迎访问我的
      <a href="https://github.com/KairuiLiu" target="_blank"><i class="fab fa-github-alt"></i> GitHub</a> .
  </p>
  <p>
      这个网站主要用来存储学习 / 瞎搞的笔记, 同时提供了邮件、图床等服务. 对于一些自认为有趣的<a href="javascript:;" id="myProj">项目</a>, 我也会将项目介绍, 文档, 甚至是设计指南发布在这里.
  </p>
  <ul>
    <li><p><a href="/artrank/" target="_blank">ArtRank</a>: 基于Node.JS的PageRank文献管理推荐系统</p></li>
    <li><p><a href="/gooj/" target="_blank">GoOJ</a>: 基于Golang的高校在线评测系统</p></li>
    <li><p><a href="/projects/" target="_blank">更多 <i class="fas fa-angle-right"></i></a></p></li>

  </ul>
  <p>
      可以在这些平台找到我: 
      <a href="https://www.zhihu.com/people/liu-kai-rui-18" target="_blank">知乎</a>,
      <a href="https://space.bilibili.com/33238144" target="_blank">bilibili</a>
      和
      <a href="https://www.coolapk.com/u/805314?from=qr" target="_blank">酷安</a>.
  </p>

  <p>
      下滑即可进入博客 <i class="fas fa-arrow-down"></i>
  </p>
</div>
</div>  

 
<script>
  var OriginTitile = document.title;
  var titleTime;
  document.addEventListener("visibilitychange", function() {
    if (document.hidden) {
      document.title = "谢谢离开 | " + OriginTitile;
      clearTimeout(titleTime);
    } else {
      document.title = "欢迎归来 | " + OriginTitile;
      titleTime = setTimeout(function() {
        document.title = OriginTitile;
      }, 2000);
    }
  });
</script>


<script>
  function checkIndex(){
    let nindexItems = [[".nindexBK","display","block","none"],[".headband","display","none","block"],["div.toggle.sidebar-toggle","visibility","hidden","visible"]]; // ,["button.darkmode-toggle","visibility","hidden","visible"]];
    let isIndex = new RegExp("^(\/){0,1}(index){0,1}(\.html){0,1}(/)*$").test(location.pathname);
    let backCanvas = document.querySelector(".nindexBK");
    nindexItems.dispShift = function (stat){this.forEach((e) => {try{document.querySelector(e[0]).style[e[1]]=e[2+stat];}catch{}})}
    if(isIndex){
      nindexItems.dispShift(0);
      window.onscroll=function(){
        if(document.documentElement.scrollTop>backCanvas.offsetHeight*1.05){
          nindexItems.dispShift(1);window.onscroll=null;
        }
      }
      document.querySelector("#myProj").onclick=()=>{
        let projlist = document.querySelector(".nindex>ul");
        projlist.className.indexOf("ulactive")==-1?projlist.classList.add("ulactive"):projlist.classList.remove("ulactive")
      }
    }else nindexItems.dispShift(1);
  }
  checkIndex();
</script>

<!--<script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>-->

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="LiuKairui Website" type="application/atom+xml">
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">LiuKairui Website</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">要有信仰</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fas fa-hashtag fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-收藏夹"><a href="/favorites/" rel="section"><i class="fab fa-gratipay fa-fw"></i>收藏夹</a></li>
        <li class="menu-item menu-item-留言板"><a href="/messageBoard/" rel="section"><i class="fab fa-facebook-messenger fa-fw"></i>留言板</a></li>
        <li class="menu-item menu-item-项目"><a href="/projects/" rel="section"><i class="fa fa-satellite fa-fw"></i>项目</a></li>
        <li class="menu-item menu-item-图床"><a href="http://img.liukairui.me/" rel="noopener" target="_blank"><i class="fa fa-bed fa-fw"></i>图床</a></li>
        <li class="menu-item menu-item-云ide"><a href="http://vsc.liukairui.me/" rel="noopener" target="_blank"><i class="fas fa-code fa-fw"></i>云IDE</a></li>
        <li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
        <li class="menu-item menu-item-邮箱"><a href="http://mail.liukairui.me/mail" rel="noopener" target="_blank"><i class="fas fa-envelope-open-text fa-fw"></i>邮箱</a></li>
        <li class="menu-item menu-item-rss"><a href="/atom.xml" rel="section"><i class="fa fa-rss fa-fw"></i>RSS</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#linux%E7%AE%80%E4%BB%8B"><span class="nav-number">1.</span> <span class="nav-text">Linux简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#linux%E7%9A%84%E5%AE%89%E8%A3%85"><span class="nav-number">2.</span> <span class="nav-text">Linux的安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%99%9A%E6%8B%9F%E6%9C%BA%E9%85%8D%E7%BD%AE"><span class="nav-number">2.1.</span> <span class="nav-text">虚拟机配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E5%88%86%E5%8C%BA"><span class="nav-number">2.2.</span> <span class="nav-text">系统分区</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%86%E5%8C%BA%E7%9A%84%E5%BD%A2%E5%BC%8F"><span class="nav-number">2.2.1.</span> <span class="nav-text">分区的形式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%86%E5%8C%BA%E7%B1%BB%E5%9E%8B"><span class="nav-number">2.2.2.</span> <span class="nav-text">分区类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%86%E5%8C%BA%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="nav-number">2.2.3.</span> <span class="nav-text">分区格式化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%BE%E5%A4%87%E6%96%87%E4%BB%B6%E5%90%8D"><span class="nav-number">2.2.4.</span> <span class="nav-text">设备文件名</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#linux%E7%9A%84%E5%AE%89%E8%A3%85-1"><span class="nav-number">2.3.</span> <span class="nav-text">Linux的安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ssh%E8%BF%9E%E6%8E%A5"><span class="nav-number">2.4.</span> <span class="nav-text">SSH连接</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9D%E5%AD%A6%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="nav-number">3.</span> <span class="nav-text">初学注意事项</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8Ewindwos%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">3.1.</span> <span class="nav-text">与WIndwos的区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%AE%A1%E7%90%86"><span class="nav-number">3.2.</span> <span class="nav-text">Linux服务器管理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#linux%E7%9A%84%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84"><span class="nav-number">3.2.1.</span> <span class="nav-text">Linux的目录结构</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="nav-number">4.</span> <span class="nav-text">常用命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%91%BD%E4%BB%A4%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A0%BC%E5%BC%8F"><span class="nav-number">4.1.</span> <span class="nav-text">命令的基本格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E5%BD%95%E6%93%8D%E4%BD%9C%E5%91%BD%E4%BB%A4"><span class="nav-number">4.2.</span> <span class="nav-text">目录操作命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C%E5%91%BD%E4%BB%A4"><span class="nav-number">4.3.</span> <span class="nav-text">文件操作命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9D%83%E9%99%90%E5%91%BD%E4%BB%A4"><span class="nav-number">4.4.</span> <span class="nav-text">权限命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#linux%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%9D%83%E9%99%90"><span class="nav-number">4.4.1.</span> <span class="nav-text">Linux的基本权限</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9D%83%E9%99%90%E5%9F%BA%E6%9C%AC%E5%91%BD%E4%BB%A4"><span class="nav-number">4.4.2.</span> <span class="nav-text">权限基本命令</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9D%83%E9%99%90%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%9C%E7%94%A8"><span class="nav-number">4.4.3.</span> <span class="nav-text">权限的基本作用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#umask%E9%BB%98%E8%AE%A4%E6%9D%83%E9%99%90"><span class="nav-number">4.4.4.</span> <span class="nav-text">umask默认权限</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%AE%E5%8A%A9%E5%91%BD%E4%BB%A4"><span class="nav-number">4.5.</span> <span class="nav-text">帮助命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%90%9C%E7%B4%A2%E5%91%BD%E4%BB%A4"><span class="nav-number">4.6.</span> <span class="nav-text">搜索命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%A1%E9%81%93%E7%AC%A6"><span class="nav-number">4.7.</span> <span class="nav-text">管道符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%AB%E5%90%8D%E4%B8%8E%E5%BF%AB%E6%8D%B7%E5%91%BD%E4%BB%A4"><span class="nav-number">4.8.</span> <span class="nav-text">别名与快捷命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8E%8B%E7%BC%A9%E4%B8%8E%E8%A7%A3%E5%8E%8B%E5%91%BD%E4%BB%A4"><span class="nav-number">4.9.</span> <span class="nav-text">压缩与解压命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%80%E5%85%B3%E6%9C%BA%E5%91%BD%E4%BB%A4"><span class="nav-number">4.10.</span> <span class="nav-text">开关机命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BD%91%E7%BB%9C%E5%91%BD%E4%BB%A4"><span class="nav-number">4.11.</span> <span class="nav-text">网络命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E7%97%95%E8%BF%B9%E5%91%BD%E4%BB%A4"><span class="nav-number">4.12.</span> <span class="nav-text">系统痕迹命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%82%E8%BD%BD%E5%91%BD%E4%BB%A4"><span class="nav-number">4.13.</span> <span class="nav-text">挂载命令</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vim%E7%BC%96%E8%BE%91%E5%99%A8"><span class="nav-number">5.</span> <span class="nav-text">Vim编辑器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BD%AF%E4%BB%B6%E5%8C%85%E5%AE%89%E8%A3%85"><span class="nav-number">6.</span> <span class="nav-text">软件包安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AF%E4%BB%B6%E5%8C%85%E7%9A%84%E5%88%86%E7%B1%BB"><span class="nav-number">6.1.</span> <span class="nav-text">软件包的分类</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%8C%85"><span class="nav-number">6.1.1.</span> <span class="nav-text">二进制包</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E8%BF%9B%E5%88%B6rpm%E5%8C%85%E5%91%BD%E4%BB%A4"><span class="nav-number">6.2.</span> <span class="nav-text">二进制RPM包命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#rpm%E5%8C%85%E7%9A%84%E5%91%BD%E5%90%8D%E8%A7%84%E5%88%99"><span class="nav-number">6.2.1.</span> <span class="nav-text">RPM包的命名规则</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rpm%E5%8C%85%E7%9A%84%E5%AE%89%E8%A3%85"><span class="nav-number">6.2.2.</span> <span class="nav-text">RPM包的安装</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rpm%E5%8C%85%E5%8D%87%E7%BA%A7"><span class="nav-number">6.2.3.</span> <span class="nav-text">RPM包升级</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rpm%E5%8C%85%E5%8D%B8%E8%BD%BD"><span class="nav-number">6.2.4.</span> <span class="nav-text">RPM包卸载</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rpm%E5%8C%85%E7%9A%84%E6%9F%A5%E8%AF%A2"><span class="nav-number">6.2.5.</span> <span class="nav-text">RPM包的查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rpm%E5%8C%85%E9%AA%8C%E8%AF%81"><span class="nav-number">6.2.6.</span> <span class="nav-text">RPM包验证</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%95%B0%E5%AD%97%E8%AF%81%E4%B9%A6"><span class="nav-number">6.2.7.</span> <span class="nav-text">数字证书</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rpm%E5%8C%85%E7%9A%84%E6%96%87%E4%BB%B6%E6%8F%90%E5%8F%96%E5%91%BD%E4%BB%A4"><span class="nav-number">6.2.8.</span> <span class="nav-text">RPM包的文件提取命令</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rpm%E5%8C%85%E5%9C%A8%E7%BA%BF%E5%AE%89%E8%A3%85%E5%91%BD%E4%BB%A4yum"><span class="nav-number">6.2.9.</span> <span class="nav-text">RPM包在线安装命令yum</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%BA%90%E7%A0%81%E5%8C%85%E7%9A%84%E5%AE%89%E8%A3%85"><span class="nav-number">6.3.</span> <span class="nav-text">源码包的安装</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%84%9A%E6%9C%AC%E5%8C%85%E5%AE%89%E8%A3%85"><span class="nav-number">6.3.1.</span> <span class="nav-text">脚本包安装</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86"><span class="nav-number">7.</span> <span class="nav-text">用户管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%94%A8%E6%88%B7%E7%9B%B8%E5%85%B3%E6%96%87%E4%BB%B6"><span class="nav-number">7.1.</span> <span class="nav-text">用户相关文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4"><span class="nav-number">7.2.</span> <span class="nav-text">用户管理命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%84%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4"><span class="nav-number">7.3.</span> <span class="nav-text">组管理命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#acl%E6%9D%83%E9%99%90"><span class="nav-number">7.4.</span> <span class="nav-text">ACL权限</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#sudo-%E6%8E%88%E6%9D%83"><span class="nav-number">7.5.</span> <span class="nav-text">sudo 授权</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%B9%E6%AE%8A%E6%96%87%E4%BB%B6%E6%9D%83%E9%99%90"><span class="nav-number">7.6.</span> <span class="nav-text">特殊文件权限</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#chattr%E6%9D%83%E9%99%90"><span class="nav-number">7.7.</span> <span class="nav-text">chattr权限</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#selinux%E6%9D%83%E9%99%90"><span class="nav-number">7.7.1.</span> <span class="nav-text">SELinux权限</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86"><span class="nav-number">8.</span> <span class="nav-text">文件系统管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A1%AC%E7%9B%98%E7%BB%93%E6%9E%84%E4%B8%8E%E6%8E%A5%E5%8F%A3"><span class="nav-number">8.1.</span> <span class="nav-text">硬盘结构与接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F"><span class="nav-number">8.2.</span> <span class="nav-text">文件系统</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90"><span class="nav-number">8.2.1.</span> <span class="nav-text">文件系统组成</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%B8%B8%E8%A7%81%E7%9A%84%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F"><span class="nav-number">8.2.2.</span> <span class="nav-text">常见的文件系统</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E7%A1%AC%E7%9B%98%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4"><span class="nav-number">8.3.</span> <span class="nav-text">常用硬盘管理命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%8B%E5%B7%A5%E5%88%86%E5%8C%BA%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">8.4.</span> <span class="nav-text">手工分区的方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%AB%98%E7%BA%A7%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86"><span class="nav-number">9.</span> <span class="nav-text">高级文件系统管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A3%81%E7%9B%98%E9%85%8D%E9%A2%9D"><span class="nav-number">9.1.</span> <span class="nav-text">磁盘配额</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#lvm%E9%80%BB%E8%BE%91%E5%8D%B7%E7%AE%A1%E7%90%86"><span class="nav-number">9.2.</span> <span class="nav-text">LVM逻辑卷管理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E5%8E%9F%E7%90%86"><span class="nav-number">9.2.1.</span> <span class="nav-text">基本原理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9B%BE%E5%BD%A2%E7%95%8C%E9%9D%A2%E4%B8%8B%E5%88%86%E5%8C%BA"><span class="nav-number">9.2.2.</span> <span class="nav-text">图形界面下分区</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8fdisk%E5%88%86%E5%8C%BA"><span class="nav-number">9.2.3.</span> <span class="nav-text">使用fdisk分区</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#shell%E5%9F%BA%E7%A1%80"><span class="nav-number">10.</span> <span class="nav-text">Shell基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#shell%E7%9A%84%E6%89%A7%E8%A1%8C%E6%96%B9%E5%BC%8F"><span class="nav-number">10.1.</span> <span class="nav-text">Shell的执行方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#shell%E7%9A%84%E5%9F%BA%E6%9C%AC%E5%8A%9F%E8%83%BD"><span class="nav-number">10.2.</span> <span class="nav-text">Shell的基本功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#shell%E7%9A%84%E5%8F%98%E9%87%8F%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">10.3.</span> <span class="nav-text">Shell的变量和运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8F%98%E9%87%8F"><span class="nav-number">10.3.1.</span> <span class="nav-text">变量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">10.3.2.</span> <span class="nav-text">运算符</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="nav-number">10.4.</span> <span class="nav-text">环境变量配置文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#shell%E7%BC%96%E7%A8%8B"><span class="nav-number">11.</span> <span class="nav-text">Shell编程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-number">11.1.</span> <span class="nav-text">正则表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E6%88%AA%E5%8F%96%E5%92%8C%E6%9B%BF%E6%8D%A2"><span class="nav-number">11.2.</span> <span class="nav-text">字符截取和替换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E5%A4%84%E7%90%86%E5%91%BD%E4%BB%A4"><span class="nav-number">11.3.</span> <span class="nav-text">字符处理命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD"><span class="nav-number">11.4.</span> <span class="nav-text">条件判断</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="nav-number">11.5.</span> <span class="nav-text">流程控制语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%B9%E6%AE%8A%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="nav-number">11.6.</span> <span class="nav-text">特殊流程控制语句</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%AF%E5%8A%A8%E5%BC%95%E5%AF%BC%E4%B8%8E%E4%BF%AE%E5%A4%8D"><span class="nav-number">12.</span> <span class="nav-text">启动引导与修复</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E7%9A%84%E8%BF%90%E8%A1%8C%E7%BA%A7%E5%88%AB"><span class="nav-number">12.1.</span> <span class="nav-text">系统的运行级别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%AF%E5%8A%A8%E5%BC%95%E5%AF%BC%E7%A8%8B%E5%BA%8F"><span class="nav-number">12.2.</span> <span class="nav-text">启动引导程序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E4%BF%AE%E5%A4%8D%E6%A8%A1%E5%BC%8F"><span class="nav-number">12.3.</span> <span class="nav-text">系统修复模式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E7%AE%A1%E7%90%86"><span class="nav-number">13.</span> <span class="nav-text">服务管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E7%9A%84%E7%AE%80%E4%BB%8B%E4%B8%8E%E5%88%86%E7%B1%BB"><span class="nav-number">13.1.</span> <span class="nav-text">服务的简介与分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#rpm%E5%8C%85%E7%9A%84%E5%90%AF%E5%8A%A8%E5%92%8C%E8%87%AA%E5%90%AF%E5%8A%A8"><span class="nav-number">13.2.</span> <span class="nav-text">RPM包的启动和自启动</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#linux%E4%B8%AD%E5%B8%B8%E8%A7%81%E7%9A%84%E6%9C%8D%E5%8A%A1%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-number">13.3.</span> <span class="nav-text">Linux中常见的服务的作用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86"><span class="nav-number">14.</span> <span class="nav-text">系统管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%9B%E7%A8%8B%E7%AE%A1%E7%90%86"><span class="nav-number">14.1.</span> <span class="nav-text">进程管理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B7%A5%E4%BD%9C%E7%AE%A1%E7%90%86"><span class="nav-number">14.2.</span> <span class="nav-text">工作管理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E8%B5%84%E6%BA%90%E6%9F%A5%E7%9C%8B"><span class="nav-number">14.3.</span> <span class="nav-text">系统资源查看</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1"><span class="nav-number">14.4.</span> <span class="nav-text">系统定时任务</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%97%A5%E5%BF%97%E7%AE%A1%E7%90%86"><span class="nav-number">15.</span> <span class="nav-text">日志管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A5%E5%BF%97"><span class="nav-number">15.1.</span> <span class="nav-text">日志</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A5%E5%BF%97%E6%9C%8D%E5%8A%A1"><span class="nav-number">15.2.</span> <span class="nav-text">日志服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A5%E5%BF%97%E8%BD%AE%E6%9B%BF"><span class="nav-number">15.3.</span> <span class="nav-text">日志轮替</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Liu Kairui"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Liu Kairui</p>
  <div class="site-description" itemprop="description">LiuKairui Persional Website</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">45</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">53</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/KairuiLiu" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;KairuiLiu" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:tclkr@live.com" title="E-Mail → mailto:tclkr@live.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://qm.qq.com/cgi-bin/qm/qr?k=oaf3ToOlN17hr5sHV98bT8qxsNYGaw5L&noverify=0" title="QQ → https:&#x2F;&#x2F;qm.qq.com&#x2F;cgi-bin&#x2F;qm&#x2F;qr?k&#x3D;oaf3ToOlN17hr5sHV98bT8qxsNYGaw5L&amp;noverify&#x3D;0" rel="noopener" target="_blank"><i class="fab fa-qq fa-fw"></i>QQ</a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-rss fa-fw"></i>RSS</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://liukairui.blog.csdn.net/" title="CSDN → https:&#x2F;&#x2F;liukairui.blog.csdn.net" rel="noopener" target="_blank"><i class="fab fa-cuttlefish fa-fw"></i>CSDN</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://liukairui.cc/" title="国内站点 → http:&#x2F;&#x2F;liukairui.cc" rel="noopener" target="_blank"><i class="fa fa-globe fa-fw"></i>国内站点</a>
      </span>
  </div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll site-overview-item animated">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://paste.ubuntu.com/" title="https:&#x2F;&#x2F;paste.ubuntu.com&#x2F;" rel="noopener" target="_blank">Ubuntu Pastebin</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://send.firefox.com/" title="https:&#x2F;&#x2F;send.firefox.com&#x2F;" rel="noopener" target="_blank">Firefox Send</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://jkwzs.cn/" title="https:&#x2F;&#x2F;jkwzs.cn&#x2F;" rel="noopener" target="_blank">南花醉笔丶の个人博客</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://ywrby.cn/" title="https:&#x2F;&#x2F;ywrby.cn&#x2F;" rel="noopener" target="_blank">Ywrby个人博客</a>
        </li>
    </ul>
  </div>

        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://liukairui.me/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Liu Kairui">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="LiuKairui Website">
      <meta itemprop="description" content="LiuKairui Persional Website">
    </span>
    
    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Linux系统管理教程笔记 | LiuKairui Website">
      <meta itemprop="description" content="介绍了Linux基础内容,主讲:尚硅谷李明与沈超,视频来自B站:BV1ut411a7ro">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Linux系统管理教程笔记
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-03-03 18:06:00" itemprop="dateCreated datePublished" datetime="2021-03-03T18:06:00+08:00">2021-03-03</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E8%BF%90%E7%BB%B4/" itemprop="url" rel="index"><span itemprop="name">运维</span></a>
        </span>
    </span>

  
    <span id="/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" class="post-meta-item leancloud_visitors" data-flag-title="Linux系统管理教程笔记" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
    <span class="post-meta-item-icon">
      <i class="far fa-comment"></i>
    </span>
    <span class="post-meta-item-text">Valine：</span>
  
    <a title="valine" href="/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

<script>
    CONFIG.hostname = location.hostname;
</script>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>84k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>1:16</span>
    </span>
</div>

            <div class="post-description">介绍了Linux基础内容,主讲:尚硅谷李明与沈超,视频来自B站:BV1ut411a7ro</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="linux简介">Linux简介</h2>
<p><strong>Unix主要发行版</strong></p>
<table>
<thead>
<tr class="header">
<th>操作系统</th>
<th>公司</th>
<th>硬件平台</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>AIX</td>
<td>IBM</td>
<td>PowerPC</td>
</tr>
<tr class="even">
<td>UX</td>
<td>HP</td>
<td>PA-RISC</td>
</tr>
<tr class="odd">
<td>Solaris</td>
<td>SUN</td>
<td>SPARC</td>
</tr>
<tr class="even">
<td>Linux,BSD</td>
<td>...</td>
<td>IA(Intel、AMD...)</td>
</tr>
</tbody>
</table>
<p><strong>常用开源软件</strong></p>
<ul>
<li>Web服务器：Apach/Nginx</li>
<li>数据库：MySQL/SQLserver/Oracle/MongoDB</li>
<li>脚本语言：PHP/Ruby/Python</li>
<li>文件服务器：Samba</li>
</ul>
<p>支撑互联网的开源技术：LAMP(Linux,Apach,MySQL,PHP)</p>
<h2 id="linux的安装">Linux的安装</h2>
<h3 id="虚拟机配置">虚拟机配置</h3>
<ul>
<li>CentOS 6 安装最小内存是628MB，实际运行还可以更小，选择1024M</li>
<li>硬盘选择20G</li>
<li>处理器数量不超过<strong>线程数</strong></li>
<li>网络配置
<ul>
<li>桥接：连接了本地真实网卡，可以连接到本机，局域网，公网。注意要手动设置桥接到那块网卡</li>
<li>NAT：连接到虚拟网卡，可以连接本机和公网</li>
<li>主机模式：连接到虚拟网卡，可以连接本机</li>
</ul></li>
</ul>
<p><strong>虚拟机快照</strong></p>
<p>可以理解为是游戏存档，可用于系统的快速恢复，系统克隆</p>
<h3 id="系统分区">系统分区</h3>
<p>为了读取数据方便，我们将硬盘逻辑上的进行了分区</p>
<h4 id="分区的形式">分区的形式</h4>
<ul>
<li>MBR分区表：主引导记录分区表，最古老的，最大支持2.1T硬盘，最多支持4分区</li>
<li>GPT分区表：全局唯一标识分区表，最大9.4ZB，无分区限制</li>
</ul>
<h4 id="分区类型">分区类型</h4>
<ul>
<li>主分区：最多四个</li>
<li>扩展分区
<ul>
<li>最多一个</li>
<li>主+扩最多四个</li>
<li>不能写入数据，只能包括逻辑分区</li>
</ul></li>
<li>逻辑分区</li>
</ul>
<p>这里的最多4个指的是MBR分区表</p>
<p>在使用MBR的时候最多4个分区可能不能满足我们使用，所以可以使用扩展分区，但是两者加起来不得超过4个，扩展分区不能写入数据，可以在里面放逻辑分区</p>
<h4 id="分区格式化">分区格式化</h4>
<p>格式化又叫逻辑格式化，目的是用于<strong>写入文件系统</strong>，在磁盘特定区域划出一片用于存放文件分配表，目录表等用于文件管理的磁盘空间，Windows下常见的文件系统有FAT16，FAT32，NTFS，Linux下常见的有EXT2，EXT3，EXT4</p>
<p>格式化会将分区划成两部分</p>
<ul>
<li>一部分用来存数据，这部分会将硬盘划成等大的数据块(默认4KB)，一个数据块如果没有写满也不能存别的数据</li>
<li>一部分用来索引，将这个索引叫做inode，一个inode为128B，存放文件的位置，时间，权限</li>
</ul>
<h4 id="设备文件名">设备文件名</h4>
<p>Linux下所有设备都有文件名，其中</p>
<ul>
<li>/dev/hda1：IDE硬盘接口</li>
<li>/dev/sda1：SCSI/STAT接口</li>
</ul>
<p>其中前两个字母(例如：sd)表示设备类型，之后的字母与数字(例如sda1的a1)表示这个类型的第几块硬盘的第几号分区，注意是第几号分区，例如有两种分区方式</p>
<ol type="1">
<li>三个主分区，分别是sda1-sda3，一个扩展分区是sda4，扩展分区有两个逻辑分区发表是sda5,sda6</li>
<li>一个主分区是sda1，一个扩展分区是sda2，扩展分区有两个逻辑分区发表是sda5,sda6</li>
</ol>
<p><strong>注意sda1-sda4永远是主分区或者扩展分区的，逻辑分区一定是sda5+</strong></p>
<p>#### 挂载点</p>
<p>使用已经存在的<strong>空目录</strong>作为挂载点</p>
<ul>
<li><p>必须分区</p>
<ul>
<li>/ (根分区)</li>
<li>swap分区(交换分区，允许你不分，但是强烈建议)
<ul>
<li>实际内存小于4G应该是2倍</li>
<li>实际内存大于4G应该与实际一致</li>
<li>实验用虚拟机不大于2G</li>
</ul></li>
</ul></li>
<li><p>推荐分区</p>
<ul>
<li><p>/boot (启动分区 1G 强烈建议)</p>
<p>当系统启动的时候，系统会临时释放很小一点文件用于启动，如果硬盘写满，这点空间都没有就无法启动，于是单独开一个boot用于启动</p></li>
</ul></li>
<li><p>常用分区</p>
<ul>
<li>/home 常用于文件服务器</li>
<li>/www 常用于Web服务器</li>
</ul></li>
</ul>
<p>挂载点的位置随意，但是/bin,/lib,/etc必须和/在同目录</p>
<h3 id="linux的安装-1">Linux的安装</h3>
<p>关于ISO文件中的"dvd1"</p>
<p>虚拟机开机之后会选择第一个安装，之后出现蓝窗口，问你找到硬盘，要不要检测里面有什么，选跳过，检测很慢，之后可能报错，说显卡有问题，此时重启或者换镜像</p>
<p>语言选择简体中文，这里的选择语言不仅仅是安装过程的语言，而是安装的系统的，不选中文就没有中文的编码与字体包</p>
<p>然后选择基本存储，主机名需要修改，否则整个集群都是一个名字，暂时不要修改</p>
<p>选择创建自定义分区</p>
<p>选择空闲-标准分区-挂载点:/home-大小2048M-固定大小-不加密，同理创建</p>
<ul>
<li>/boot 200M boot必须是启动分区，有他必须是sda1，不能是sda2</li>
<li>/ 15G</li>
</ul>
<p>创建交换分区：文件系统类型为swap不需要挂载点</p>
<p>真实的服务器选minimal，此时为了学习选择BasicServer</p>
<p>之后会自动安装</p>
<h3 id="ssh连接">SSH连接</h3>
<p>若是VirtualBox则务必设置为NAT端口转发，在高级中选择端口转发添加规则，客户机端口为22，主机端口选择不用的</p>
<p>开机后<code>ifconfig</code>看看有没有检测到非lo的网卡，没有的话</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">ip</span> a					<span class="token comment"># 查看新网卡</span>
<span class="token builtin class-name">cd</span> /etc/sysconfig/network-scripts/
<span class="token function">vim</span> ifcfg-eth0			<span class="token comment"># eth0换成新网卡</span>
<span class="token comment"># 将ONBOOT改为yes</span>
<span class="token function">service</span> network restart <span class="token comment"># 重启网络服务</span></code></pre>
<p>之后在主机上进行ssh连接，<strong>linux</strong>为</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">ssh</span> -l <span class="token punctuation">[</span>CentOsUsername<span class="token punctuation">]</span> -p <span class="token number">1111</span> <span class="token number">127.0</span>.0.1 <span class="token comment"># 将1111替换为当时端口转发的主机端口</span></code></pre>
<p>windows直接远程工具</p>
<h2 id="初学注意事项">初学注意事项</h2>
<h3 id="与windwos的区别">与WIndwos的区别</h3>
<ul>
<li>Linux严格区分大小写，没有大写命令，命令的选项有大写</li>
<li>Linux一切皆文件，硬件也是文件，永久保存的配置必须写入文件</li>
<li>Linux不靠扩展名区分文件类型，靠权限标识识别文件类型，但是部分特殊文件还是要求写扩展名以区分类型，有
<ul>
<li>压缩包：如果压缩包没有扩展名管理员不知道用哪个命令去解压缩压缩包</li>
<li>二进制软件包：redhat下软件包用.rpm，debian下一般是.deb</li>
<li>程序文本：shell的.sh</li>
<li>网页文件：.htlp，.php...是网页服务器要求的</li>
</ul></li>
<li>所有的存储设备必须挂载后使用</li>
</ul>
<h3 id="linux服务器管理">Linux服务器管理</h3>
<h4 id="linux的目录结构">Linux的目录结构</h4>
<table>
<colgroup>
<col style="width: 14%" />
<col style="width: 85%" />
</colgroup>
<thead>
<tr class="header">
<th>目录名</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>/bin/</td>
<td>存放系统命令的目录，所有用户都可以执行，是/usr/bin/的软链接</td>
</tr>
<tr class="even">
<td>/sbin/</td>
<td>存放系统命令的目录，root可以执行，是/usr/sbin/的软链接</td>
</tr>
<tr class="odd">
<td>/usr/bin/</td>
<td>存放系统命令的目录，所有用户都可以执行</td>
</tr>
<tr class="even">
<td>/usr/sbin/</td>
<td>存放系统命令的目录，root可以执行</td>
</tr>
<tr class="odd">
<td>/boot/</td>
<td>系统启动目录。存放与启动相关文件如内核与启动引导(grub)等文件</td>
</tr>
<tr class="even">
<td>/dev/</td>
<td>设备文件保存位置(例如分区)</td>
</tr>
<tr class="odd">
<td>/etc/</td>
<td>保存配置文件(默认安装的程序的配置文件)</td>
</tr>
<tr class="even">
<td>/home/</td>
<td>普通用户的家目录</td>
</tr>
<tr class="odd">
<td>/root/</td>
<td>root用户的家目录</td>
</tr>
<tr class="even">
<td>/lib/</td>
<td>系统的函数库(频繁调用的程序库，例如C的iostream)</td>
</tr>
<tr class="odd">
<td>/lib64/</td>
<td>系统的64位函数库(只要看到.so.数字的都是函数库)</td>
</tr>
<tr class="even">
<td>/lost+find/</td>
<td>当系统意外关机之后产生的碎片文件，再次开机如果可以恢复系统会自动恢复</td>
</tr>
<tr class="odd">
<td>/media/</td>
<td>空目录，事先准备的挂载点，一般用于挂光盘</td>
</tr>
<tr class="even">
<td>/misc/</td>
<td>空目录，事先准备的挂载点，一般用于挂网络设备</td>
</tr>
<tr class="odd">
<td>/mnt/</td>
<td>空目录，事先准备的挂载点，一般用于挂硬盘</td>
</tr>
<tr class="even">
<td>/opt/</td>
<td>软件安装位置(用的少，还是习惯在/usr/local)</td>
</tr>
<tr class="odd">
<td>/proc/</td>
<td>不是硬盘的挂载点，里面挂载的是内存</td>
</tr>
<tr class="even">
<td>/sys/</td>
<td>不是硬盘的挂载点，里面挂载的是内存</td>
</tr>
<tr class="odd">
<td>/net/</td>
<td>不是硬盘的挂载点，里面挂载的是内存</td>
</tr>
<tr class="even">
<td>/selinux/</td>
<td>linux的增强安全组件，用来限制root</td>
</tr>
<tr class="odd">
<td>/usr/</td>
<td>Unix Software
Resource系统软件资源目录的缩写，存放系统资源文件的目录</td>
</tr>
<tr class="even">
<td>/var/</td>
<td>存放动态数据的目录</td>
</tr>
<tr class="odd">
<td>/tmp/</td>
<td>临时文件</td>
</tr>
<tr class="even">
<td>/usr/lib/</td>
<td>应用程序调用函数库保存位置，和/lib/的不一样</td>
</tr>
<tr class="odd">
<td>/usr/local/</td>
<td>程序安装位置</td>
</tr>
<tr class="even">
<td>/usr/share/</td>
<td>资源文件保存位置，例如文档，说明文档，字体目录</td>
</tr>
<tr class="odd">
<td>/usr/src/</td>
<td>源码包保存位置，不过一般是放内核源码，建议三方的源码一般放/usr/local/src，内核放在/usr/src/kernels</td>
</tr>
<tr class="even">
<td>/usr/src/kernels/</td>
<td>建议将内核源码放这里</td>
</tr>
<tr class="odd">
<td>/var/log/</td>
<td>系统日至目录</td>
</tr>
<tr class="even">
<td>/var/lib/</td>
<td>程序运行时数据保存目录</td>
</tr>
<tr class="odd">
<td>/var/lib/mysql/</td>
<td>MySQL数据库保存位置</td>
</tr>
<tr class="even">
<td>/var/www/html/</td>
<td>默认的apach网页目录</td>
</tr>
<tr class="odd">
<td>/var/run/</td>
<td>服务启动后他们的PID的保存位置，是/run/的软链接</td>
</tr>
<tr class="even">
<td>/var/spool/</td>
<td>放置队列数据的目录，例如邮件队列与打印队列</td>
</tr>
<tr class="odd">
<td>/var/spool/mail/</td>
<td>邮件队列，收到的邮件会保存在此处</td>
</tr>
<tr class="even">
<td>/var/spool/cron/</td>
<td>系统定时任务队列的存储位置</td>
</tr>
</tbody>
</table>
<ul>
<li><p>有bin的是二进制命令文件是所有人都能执行的，sbin是管理员执行的，bin与/usr/bin目前几乎没有区别</p></li>
<li><p><code>*.so.数字</code>的文件是Linux的重要函数库</p></li>
<li><p>Linux准备了三个点挂载，但是习惯于用/mnt/</p></li>
</ul>
<p>#### 远程服务器注意事项</p>
<ul>
<li>服务器只能重启不能关机</li>
<li>重启之前要终止正在执行的任务</li>
<li>重启建议选用<code>shutdown -r now</code>并且在重启前多次执行<code>sync</code>命令，他可以将内存中的数据同步在硬盘上</li>
<li>不在访问 高峰进行高负载命令</li>
<li>配置防火墙的时候不要把自己踢出服务器，有个笨办法是在每次配置防火墙之前开定时人物没10min删除防火墙配置，确认配置没问题之后再关闭定时人物</li>
</ul>
<h2 id="常用命令">常用命令</h2>
<h3 id="命令的基本格式">命令的基本格式</h3>
<ol type="1">
<li><p>命令提示符</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@bogon ~<span class="token punctuation">]</span><span class="token comment"># </span></code></pre>
<p>第一部分是登陆的用户，例如目前是root，<span class="citation"
data-cites="是分隔符无意义">@是分隔符无意义</span>，bogon是当前系统的简写主机名，~是当前的路径，之后是命令提示符<code>#</code>或<code>$</code>,<code>#</code>表示是root用户，<code>$</code>表示是普通用户</p></li>
<li><p>命令的基本格式</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">命令 <span class="token punctuation">[</span>选项<span class="token punctuation">]</span> <span class="token punctuation">[</span>参数<span class="token punctuation">]</span></code></pre>
<p>[]表示可选，选项用于调整命令的功能，参数用来指定操作对象，不写就执行默认参数，例如<code>ls -l</code>表示长格式输出</p></li>
<li><p><code>ls -l</code>的输出解释</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@bogon ~<span class="token punctuation">]</span><span class="token comment"># ls -l</span>
总用量 <span class="token number">44</span>
-rw-------. <span class="token number">1</span> root root  <span class="token number">1284</span> <span class="token number">1</span>月  <span class="token number">22</span> <span class="token number">20</span>:31 anaconda-ks.cfg
-rw-r--r--. <span class="token number">1</span> root root <span class="token number">28250</span> <span class="token number">1</span>月  <span class="token number">22</span> <span class="token number">20</span>:31 install.log
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">7572</span> <span class="token number">1</span>月  <span class="token number">22</span> <span class="token number">20</span>:30 install.log.syslog</code></pre>
<ul>
<li>第一列表示权限</li>
<li>第二列是引用计数，对于文件表示有多少硬链接的个数，对文件夹表示有多少个一级子目录</li>
<li>第三列表示文件所有者</li>
<li>第四列表示文件所属组，分配所有者与所属组是为了方便赋权限</li>
<li>第五列文件字节(默认Byte)</li>
<li>第六列是文件修改/访问时间</li>
</ul></li>
<li><p><code>ls</code>的其他选项</p>
<ul>
<li><code>-h</code> 以易于阅读的格式输出文件大小，例如KB</li>
<li><code>-a</code> 显示隐藏文件</li>
<li><code>-d</code>
显示目录本身而不是子文件<code>ls /</code>与<code>ls -d /</code></li>
<li><code>-i</code> 显示文件inode节点号</li>
</ul></li>
<li><p>隐藏文件的作用：告诉你不要动而不是想藏起来</p></li>
<li><p>Linux中绝大部分选项没有先后顺序，例如<code>ls -al</code>与<code>ls -la</code></p></li>
</ol>
<h3 id="目录操作命令">目录操作命令</h3>
<ol type="1">
<li><p><code>ls</code>命令</p></li>
<li><p><code>cd</code>命令</p>
<p>用于切换所在的目录，注意区分绝对与相对路径，Linux中特殊路径符号有：<code>~</code>家目录<code>-</code>上次所在目录<code>.</code>当前目录<code>..</code>上一级目录</p>
<p><code>pwd</code>显示当前目录</p>
<p><code>cd</code>可以直接回到家目录</p></li>
<li><p><code>mkdir</code>命令</p>
<ul>
<li><code>mkdir tmp</code>建立目录tmp</li>
<li><code>mkdir -p ./1/2/3</code> 使用-p选项递归建立目录</li>
</ul></li>
<li><p><code>rmdir</code>命令</p>
<ul>
<li><code>rmdir</code>只能删除空目录，所以不推荐使用</li>
</ul></li>
<li><p><code>rm</code>命令</p>
<ul>
<li><code>rm</code>删除文件(夹)</li>
<li><code>-r</code>递归删除</li>
<li><code>-f</code>不提示是否确认，强制删除</li>
<li>防止误删除建议安装<code>extundelete</code></li>
</ul></li>
</ol>
<h3 id="文件操作命令">文件操作命令</h3>
<ol type="1">
<li><p><code>touch</code>命令</p>
<ul>
<li>用于创建新文件或者修改文件时间，也就是没有就创建，有就修改文件时间</li>
<li><code>touch 文件名</code>就创建了文件</li>
</ul></li>
<li><p><code>stat</code>命令</p>
<ul>
<li><p>查看文件的基本信息，结果如下</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># stat abc</span>
  File: <span class="token string">"abc"</span>
  Size: <span class="token number">0</span>               Blocks: <span class="token number">0</span>          IO Block: <span class="token number">4096</span>   普通空文件
Device: 802h/2050d      Inode: <span class="token number">393882</span>      Links: <span class="token number">1</span>
Access: <span class="token punctuation">(</span>0644/-rw-r--r--<span class="token punctuation">)</span>  Uid: <span class="token punctuation">(</span>    <span class="token number">0</span>/    root<span class="token punctuation">)</span>   Gid: <span class="token punctuation">(</span>    <span class="token number">0</span>/    root<span class="token punctuation">)</span>
Access: <span class="token number">2021</span>-01-26 <span class="token number">22</span>:13:51.117999672 +0800
Modify: <span class="token number">2021</span>-01-26 <span class="token number">22</span>:13:51.117999672 +0800
Change: <span class="token number">2021</span>-01-26 <span class="token number">22</span>:13:51.117999672 +0800</code></pre>
<p>分别是文件名，大小，占用块，每个块的大小，硬件设备标识号，inode，硬链接数目，权限，所有者ID，所属组ID，访问时间，修改时间，状态修改时间(修改权限的时间)</p></li>
</ul></li>
<li><p><code>cat</code>命令</p>
<ul>
<li>查看文件内容</li>
<li><code>-n</code>查看行号</li>
<li><code>-v</code>列出特殊字符(例如<code>\n</code>显示为<code>$</code>)</li>
<li><code>-A</code>显示所有的特殊字符</li>
<li><code>cat</code>会输出所有内容，但是终端最大行数有限，不建议使用</li>
</ul></li>
<li><p><code>more</code>命令</p>
<ul>
<li>分屏显示文件内容</li>
<li><code>空格</code>向下翻页</li>
<li><code>/字符串</code>向下搜索字符串</li>
<li><code>b</code>向上翻页</li>
<li><code>回车</code>向下一行</li>
<li><code>q</code> 推出</li>
</ul></li>
<li><p><code>less</code>命令</p>
<ul>
<li>分行显示文件内容</li>
<li><code>上下键</code>上下显示</li>
</ul></li>
<li><p><code>head</code>命令</p>
<ul>
<li>显示文件头几行</li>
<li><code>-数字</code>显示文件头几行，不写是10行</li>
</ul></li>
<li><p><code>tail</code>命令</p>
<ul>
<li><p>显示文件头几行</p></li>
<li><p><code>-数字</code>显示文件头几行，不写是10行</p></li>
<li><p><code>-f</code>监听文件变化，即当有程序向后面追加内容是自动显示变化，使用<code>ctrl+C</code>退出</p></li>
</ul></li>
<li><p><code>ln</code>命令</p>
<ul>
<li><code>ln filename ./it/is/path/</code>实现在<code>./it/is/path/</code>下创建<code>filename</code>的链接</li>
<li><code>-s</code>创建软链接，不写默认硬链接</li>
</ul></li>
<li><p>软链接与硬链接</p></li>
</ol>
<p>我们知道block块放的是文件的内容，inode指向了文件的block地址，但是文件名不知道在那里，实际上文件名存在了他的父文件夹的block中，父文件夹的block里面有个文件名-inode的表，就是说查找文件的时候实际上是去文件夹的block中查找，按照这个说法<code>/</code>的文件名-inode映射没地方存了，实际上<code>/</code>的inode是固定的，是2(1在启动的时候被占用了)</p>
<p>创建硬链接后可以看到</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token number">18</span> -rw-rw-r--. <span class="token number">2</span> liukairui liukairui <span class="token number">0</span> <span class="token number">1</span>月  <span class="token number">29</span> <span class="token number">22</span>:25 bcd
<span class="token number">18</span> -rw-rw-r--. <span class="token number">2</span> liukairui liukairui <span class="token number">0</span> <span class="token number">1</span>月  <span class="token number">29</span> <span class="token number">22</span>:25 ./bcd_h</code></pre>
<p>两个文件的inode号相同，引用系数变为2，删除原文件/硬链接之后另一个文件正常存在，原因是硬链接与原文件同inode，删除文件只是删除了父文件夹的文件名-inode的一个映射，但是另一个映射不变</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">rm</span> ./bcd
<span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">ls</span> -ild  ./bcd_h 
<span class="token number">18</span> -rw-rw-r--. <span class="token number">1</span> liukairui liukairui <span class="token number">3</span> <span class="token number">1</span>月  <span class="token number">29</span> <span class="token number">22</span>:30 ./bcd_h</code></pre>
<p>注意的是，不推荐使用硬链接，因为</p>
<ul>
<li><p>系统不会明显的标注一个链接是硬链接，只能通过同inode与应用系数增加判断是硬链接</p></li>
<li><p>普通用户不能硬链接目录，见</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">ln</span> -db --help
	-d, -F, --directory           创建指向目录的硬链接<span class="token punctuation">(</span>只适用于超级用户<span class="token punctuation">)</span></code></pre>
<p>如果引入了对目录的硬连接就有可能在目录中引入循环，那么在目录遍历的时候系统就会陷入无限循环当中。也许您会说，符号连接不也可以引入循环吗，那么为什么不限制目录的符号连接呢？原因就在于在linux系统中，每个文件(目录也是文件)都对应着一个inode结构，其中inode数据结构中包含了文件类型(目录，普通文件，符号连接文件等等)的信息，也就是说操作系统在遍历目录时可以判断出符号连接，既然可以判断出符号连接当然就可以采取一些措施来防范进入过大的循环了，系统在连续遇到8个符号连接后就停止遍历，这就是为什么对目录符号连接不会进入死循环的原因了。但是对于硬连接，由于操作系统中采用的数据结构和算法限制，目前是不能防范这种死循环的。</p>
<p>在说明第二个原因之前，我们先来看看文件的dentry结构在系统空间中长什么样子和它们是怎么存放在系统空间的。dentry结构主要包含了文件名，文件的inode
号，指向父目录dentry结构的指针和其他一些与本次讨论无关的指针，这里关键是那个指向父目录的指针；系统中所有的dentry结构都是按杂凑值存放在杂凑表中的，这里的杂凑算法很重要，它是取文件名和文件的父目录dentry结构的地址一起杂凑运算出杂凑值的。现在我们假设有两个目录
/a和/b，其中/b是我们通过ln
-d命令建立起来的对/a的硬连接。这个时候内核空间中就会存在一个/a的dentry结构和一个/b的dentry结构，由上面的知识可知，/a和/b
目录下面的每一个文件或目录都各自有对应的dentry结构(因为虽然/a目录下面的文件名没有改变，但是因为dentry结构有指向父目录dentry
的指针和计算杂凑值时考虑了父目录dentry结构的地址，这个时候dentry结构就分身乏术了)，而且这种继承还会影响到所有子目录下面的文件，这样下来就会浪费很多系统空间了，特别是如果被硬连接的目录中存在大量文件和子目录的时候就更加明显了。</p></li>
<li><p>只能同分区链接</p></li>
</ul>
<p>软链接完全可以理解为windows下的快捷方式</p>
<ul>
<li><p>删除原文件快捷方式就没了</p></li>
<li><p>在ls软链接的时候会提示原文件位置，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">ln</span> -s bcd bcd_s
<span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">ls</span> -ild bcd bcd_s
<span class="token number">18</span> -rw-rw-r--. <span class="token number">1</span> liukairui liukairui <span class="token number">0</span> <span class="token number">1</span>月  <span class="token number">29</span> <span class="token number">22</span>:49 bcd
<span class="token number">19</span> lrwxrwxrwx. <span class="token number">1</span> liukairui liukairui <span class="token number">3</span> <span class="token number">1</span>月  <span class="token number">29</span> <span class="token number">22</span>:49 bcd_s -<span class="token operator">></span> bcd</code></pre>
<p>删除原文件之后重新ls会发现bcd链接地址变成黑底白字闪烁</p></li>
<li><p>软链接的权限是最大的例如上面的<code>19 lrwxrwxrwx.</code>，意思是所有人可以访问软链接但是软链接没数据还要访问原文件，权限还要看原文件</p></li>
<li><p><strong>软链接一定要写绝对路径，否则出去或者拷贝软链接就找不到了</strong></p></li>
</ul>
<p>### 文件与目录命令</p>
<ul>
<li><p><code>rm</code>命令</p>
<ul>
<li>删除文件或者目录</li>
<li><code>-f</code>强制删除</li>
<li><code>-r</code>递归删除，目录操作使用</li>
<li><code>-i</code>交互删除，在删除之前会询问用户(默认就是交互删除)</li>
</ul></li>
<li><p><code>cp</code>命令</p>
<ul>
<li><code>cp 原文件 新文件的路径</code>在路径下复制同名新文件</li>
<li><code>cp 原文件 新文件的路径/新文件名</code>在路径下复制改名新文件</li>
<li><code>-r</code> 复制目录使用</li>
<li><code>-a</code>相当于<code>-dpr</code>目标与原文件一模一样，包括修改时间等</li>
<li><code>-i</code>覆盖同名文件</li>
</ul></li>
<li><p><code>mv</code>命令</p>
<ul>
<li><p>移动文件/重命名</p></li>
<li><p><code>-f</code>强制覆盖</p></li>
<li><p><code>-i</code>交互移动，默认就是交互</p></li>
<li><p><code>-v</code>显示详细信息，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">mv</span> -v ./cp1 ./cp2
<span class="token string">"./cp1"</span> -<span class="token operator">></span> <span class="token string">"./cp2"</span></code></pre></li>
</ul></li>
</ul>
<h3 id="权限命令">权限命令</h3>
<h4 id="linux的基本权限">Linux的基本权限</h4>
<p><code>ls -l</code>之后会看到文件的权限，形如：<code>-rw-rw-r--.</code></p>
<ul>
<li><p>第一位表示文件类型</p>
<ul>
<li><code>-</code>表示<strong>普通文件</strong></li>
<li><code>d</code>表示<strong>目录文件</strong>(目录也是文件)</li>
<li><code>l</code>表示<strong>软链接</strong></li>
<li><code>b</code>表示块设备文件，例如<code>/dev/sda</code></li>
<li><code>c</code>表示字符设备，例如键盘</li>
<li><code>p</code>管道符文件</li>
<li><code>s</code>套接字文件，一些服务支持socket访问会产生这样的文件</li>
</ul></li>
<li><p>第2-4位表示文件<strong>所有者u</strong>的权限</p>
<ul>
<li><code>r</code>表示有读权限</li>
<li><code>w</code>表示有写权限</li>
<li><code>x</code>表示有执行权限</li>
</ul></li>
<li><p>第5-7位表示文件<strong>所属组g</strong>的权限</p></li>
<li><p>第9-10位表示文件<strong>其他人x</strong>的权限</p></li>
<li><p>最后的<code>.</code>表示这个文件是被SELinux管理(保护)的</p></li>
</ul>
<h4 id="权限基本命令">权限基本命令</h4>
<ul>
<li><code>chmod</code>命令
<ul>
<li>修改文件的权限模式</li>
<li><code>chmod [选项] 权限 文件名</code>
<ul>
<li>权限可以写<code>+/-</code>，写<code>u+x</code>表示为u(所有者)添加x(执行)权限</li>
<li>权限可以用<code>,</code>连，写<code>u+x,g-x</code>表示为u(所有者)添加x(执行)权限,同时为所属组去掉执行权限</li>
<li>权限可以用数字表示，<code>r=4,w=2,x=1</code>直接把需要的加起来就可以了，例如<code>755</code>表示<code>rwxr-xr-x</code>，常见的数字权限有<code>755</code><strong>文件的执行权限与目录的基本权限</strong><code>644</code><strong>文件的基本权限</strong><code>777</code>最大权限，实际生产中不得赋予</li>
</ul></li>
<li><code>-R</code>递归设置，给子目录文件设置</li>
</ul></li>
<li><code>chown</code>命令
<ul>
<li>修改所有者与所属组</li>
<li><code>chown 新所有者 文件</code></li>
<li><code>chown 新所有者:新所属组 文件</code></li>
</ul></li>
<li><code>chgrp</code>命令
<ul>
<li>修改所属组</li>
<li><code>chgrp 新所属组 文件</code></li>
</ul></li>
<li>普通用户可以修改自己所有/所属文件权限，但是不能修改任何文件的所有者/所属组</li>
</ul>
<h4 id="权限的基本作用">权限的基本作用</h4>
<p><strong>权限对文件的作用</strong></p>
<ul>
<li>r：一旦文件具有的读权限就可以读取数据，例如cat,more,less,head,tail等命令</li>
<li>w：一旦文件具有写权限就可以修改文件内容使用例如<code>vim</code>,<code>echo &gt;&gt;</code>写入，<strong>拥有w权限并不能删除文件，想要删除文件必须获得文件父文件夹的w权限</strong></li>
<li>x：代表文件有执行的权限，但是到底能不能正确执行或者能不能执行还要看文件本身</li>
</ul>
<p><strong>权限对目录的作用</strong></p>
<ul>
<li>r：可以读目录，即可以<code>ls</code></li>
<li>w：可以修改目录下的数据例如<code>touch</code>，<code>rm</code>，<code>cp</code>，<code>mv</code>等等，这是目录的最高权限</li>
<li>x：目录不能执行，目录拥有x权限就可以对目录进行cd,进入目录</li>
<li>当然如果是<code>rw-</code>这样没有x不能进去根本没有用，所以目录至少有x</li>
</ul>
<h4 id="umask默认权限">umask默认权限</h4>
<p>当一个文件新建的时候，会有一个默认的权限，这个权限就是从umask权限获取的，Linux是通过umask得到的一个文件的默认权限，使用<code>umask</code>可以获取到系统的umask值。</p>
<ul>
<li>对于系统说文件的默认最大权限是666(系统认为x是危险的，想要作为执行文件必须后期修改)，文件夹是777</li>
<li>系统计算权限的方法是进行二进制的与非运算，对于手算可以采用先换成字母然后减去，例如umask是033，创建文件的时候默认是666是rw-rw-rw-减去033是----wx-wx就成了rw-r--r--</li>
</ul>
<p>可以修改/etc/profile修改umask，这个文件是Linux环境变量</p>
<h3 id="帮助命令">帮助命令</h3>
<ul>
<li><p><code>man</code>命令</p>
<ul>
<li><p><code>man 指令</code></p></li>
<li><p>可以使用上下箭头，PgUp/PgDn翻页，g移动到第一页，G文件尾，q退出，/查找，?反向搜索，n/N前后一个</p></li>
<li><p>帮助是有级别的，可以在man的左上角看到，其中</p>
<ul>
<li>1级别表示普通用户可以执行的命令的帮助</li>
<li>2级别表示内核调用函数的帮助</li>
<li>3级别表示C语言的函数和工具的帮助</li>
<li>4级别表示设备和特殊文件的帮助</li>
<li>5级别表示配置文件的帮助</li>
<li>6级别表示游戏的帮助</li>
<li>7级别表示杂项的帮助</li>
<li>8级别表示root可以执行命令的帮助</li>
<li>9级别表示内核的帮助</li>
</ul>
<p>可以使用<code>whatis 命令</code>/<code>man -f 命令</code>查询命令的所有帮助，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ whatis <span class="token function">passwd</span>
<span class="token function">passwd</span>               <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>  - update user's authentication tokens
<span class="token function">passwd</span>               <span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>  - password <span class="token function">file</span>
<span class="token function">passwd</span> <span class="token punctuation">[</span>sslpasswd<span class="token punctuation">]</span>   <span class="token punctuation">(</span>1ssl<span class="token punctuation">)</span>  - compute password hashes</code></pre>
<p>可以使用<code>man 级别 命令</code>打开对应级别的命令，例如<code>man 5 passwd</code></p>
<p>如果whatis不能用可以执行<code>makewhatis</code>来重建whatis</p></li>
<li><p><code>-k</code>查询所有带有指定关键字的命令</p></li>
</ul></li>
<li><p><code>info</code>命令</p>
<ul>
<li><code>info 指令</code></li>
<li>info是一本书，每个命令是一节，其中命令的某些内容又是一个小节，可以用
<ul>
<li>可以使用上下箭头，PgUp/PgDn翻页</li>
<li>在前有<code>*</code>的一行回车进入小节</li>
<li>tab在小节之间切换</li>
<li>u进入父小节</li>
<li>n/p进入下一小节/上一小节</li>
<li>?显示帮助</li>
<li>q退出</li>
</ul></li>
</ul></li>
<li><p><code>help</code>命令</p>
<ul>
<li>获取shell内置指令的帮助</li>
</ul></li>
<li><p><code>--help</code>选项</p>
<ul>
<li><code>大多数指令 --help</code>可以获取简明帮助</li>
</ul></li>
</ul>
<h3 id="搜索命令">搜索命令</h3>
<ul>
<li><p><code>whereis</code>命令</p>
<ul>
<li>直接加命令的名字，只能搜索系统命令，不能搜索文件</li>
<li>显示命令的源文件与帮助文件的位置</li>
</ul></li>
<li><p><code>which</code>命令</p>
<ul>
<li>查找系统命令的时候如果有别名也可以查到,例如</li>
</ul>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">whereis</span> <span class="token function">ls</span>
ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz
<span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">which</span> <span class="token function">ls</span>
<span class="token builtin class-name">alias</span> <span class="token assign-left variable">ls</span><span class="token operator">=</span><span class="token string">'ls --color=auto'</span>
        /bin/ls</code></pre>
<p>whereis会提示ls的位置在/bin/ls帮助文档在usr/share/man/man1p/ls.1p.gz
/usr/share/man/man1/ls.1.gz</p>
<p>which提示ls是<code>'ls --color=auto'</code>的别名，可以使用<code>alias</code>查询所有别名</p></li>
<li><p><code>locate</code>命令</p>
<ul>
<li><p>可以直接加 文件名搜索普通文件</p></li>
<li><p>他是按照数据库搜索的所以速度快，消耗小，数据库位置在<code>/var/lib/mlocate/mlocate.db</code></p></li>
<li><p>但是只能按照文件名搜索不能做按照权限、大小、时间等搜索</p></li>
<li><p>由于他是按照数据库搜索的，所以新建的文件在升级数据库之间是找不到的，可以使用命令<code>updatedb</code>升级数据库</p></li>
<li><p>有的时候发现updatedb之后还是搜索不到，这是因为locate配置文件的问题，可以编辑<code>/etc/updatedb.config</code></p>
<pre class="language-none"><code class="language-none">PRUNE_BIND_MOUNTS &#x3D; &quot;yes&quot;			# 表示文件生效
PRUNEFS &#x3D; &quot;9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs&quot;		# 不搜索文件类型，例如光驱中的文件
PRUNENAMES &#x3D; &quot;.git .hg .svn&quot;		# 不搜索扩展名为这些的文件
PRUNEPATHS &#x3D; &quot;&#x2F;afs &#x2F;media &#x2F;net &#x2F;sfs &#x2F;tmp &#x2F;udev &#x2F;var&#x2F;cache&#x2F;ccache &#x2F;var&#x2F;spool&#x2F;cups &#x2F;var&#x2F;spool&#x2F;squid &#x2F;var&#x2F;tmp&quot;			# 不搜索这些系统目录</code></pre></li>
</ul></li>
<li><p><code>find</code>命令</p>
<ul>
<li><p>按照文件名搜索[完全匹配]，格式是<code>find 搜索范围路径 -name 文件名</code></p>
<ul>
<li>按照文件名不区分大小写，格式是<code>find 搜索范围路径 -iname 文件名</code></li>
<li>按照inode搜索，格式是<code>find 搜索范围路径 -inum inode号</code></li>
</ul></li>
<li><p>可以按照文件大小搜索，格式是<code>find 范围 -size [+/-]大小</code>其中+-分别表示大于或者小于，不写就是等于，大小要写单位，KB是<code>k</code>MB是<code>M</code>，注意大小写，可以看find的帮助文档</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">-size n<span class="token punctuation">[</span>cwbkMG<span class="token punctuation">]</span>File  uses n <span class="token function">units</span> of space, rounding up.  The following suffixes can be
    used:

    <span class="token variable"><span class="token variable">`</span>b'    <span class="token keyword">for</span> <span class="token number">512</span>-byte blocks <span class="token punctuation">(</span>this is the default <span class="token keyword">if</span> no suffix is used<span class="token punctuation">)</span>
    单位b表示是512字节，也就是说1b代表512Byte,b是默认呢单位
    <span class="token variable">`</span></span>c<span class="token string">'    for bytes
    单位c表示是字节Byte
    `w'</span>    <span class="token keyword">for</span> two-byte words
	单位w是2Byte
    <span class="token variable"><span class="token variable">`</span>k'    <span class="token keyword">for</span> kibibytes <span class="token punctuation">(</span>KiB, <span class="token function">units</span> of <span class="token number">1024</span> bytes<span class="token punctuation">)</span>
    单位k是1024Byte注意小写
    <span class="token variable">`</span></span>M<span class="token string">'    for mebibytes (MiB, units of 1024 * 1024 = 1048576 bytes)
    单位M是大写
    `G'</span>    <span class="token keyword">for</span> gibibytes <span class="token punctuation">(</span>GiB, <span class="token function">units</span> of <span class="token number">1024</span> *  <span class="token number">1024</span>  *  <span class="token number">1024</span>  <span class="token operator">=</span>  1073741824bytes<span class="token punctuation">)</span>
    单位G是大写</code></pre></li>
<li><p>可以按照文件修改时间搜索，格式是<code>find 范围 -标签 [+-]时间</code></p>
<ul>
<li><code>-atime</code>是按照文件访问时间搜索</li>
<li><code>-mtime</code>是按照文件数据修改时间搜索</li>
<li><code>-ctime</code>是按照文件状态修改时间搜索</li>
<li>3，-3，+3代表第五天前修改，3天内修改，4天前修改的</li>
</ul></li>
<li><p>按照文件权限搜索</p>
<ul>
<li><code>find 范围 -perm xxx</code>查找权限恰好为那几个数字的文件</li>
<li><code>find 范围 -perm +xxx</code>查找文件的三个权限中有任何一个权限比给出的大，例如666可以查到700文件</li>
<li><code>find 范围 -perm -xxx</code>查找文件的三个权限都比给出的大，例如666不能查到700文件</li>
</ul></li>
<li><p>按照文件的所有者/所属组查询</p>
<ul>
<li><code>find 路径 [选项] 内容</code>，选项有</li>
<li><code>-uid</code>按照用户ID查询</li>
<li><code>-gid</code>按照用户组ID查询</li>
<li><code>-user</code>按照用户名查询</li>
<li><code>-group</code>按照用户组名查询</li>
<li><code>-nouser</code>查询没有所有者的文件，只有一种情况没有所有者，当文件来自windows</li>
</ul></li>
<li><p>按照文件类型搜索</p>
<ul>
<li><code>find 路径 -type d/f/l</code>分别表示目录，普通文件，软链接</li>
</ul></li>
<li><p>逻辑运算符</p>
<ul>
<li><code>-a</code> 与运算</li>
<li><code>-o</code>或运算</li>
<li><code>-not</code>非运算</li>
</ul>
<p>例如搜索大于1K的文件夹就是</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">find</span> ./ -size +1k -a -type d</code></pre></li>
<li><p><code>-exec &#123;&#125; \;</code>选项</p>
<ul>
<li>将find的结果作为参数执行到吓一个目录</li>
<li><code>find ... -exec 新命令 &#123;&#125; \;</code>格式</li>
<li><code>find ./ -size +1k -exec ls -alh &#123;&#125; \;</code>就是将find查询到的大于1k的结果放在ls的大括号里面执行，相当于ls了所有大于1k的文件，此时第二个目录不能使用别名</li>
</ul></li>
<li><p><code>-ok &#123;&#125; \;</code>选项</p>
<ul>
<li>与-exec相同，但是在每执行一个文件的时候都会询问是否执行</li>
</ul></li>
</ul></li>
<li><p><code>grep</code>命令</p>
<ul>
<li><code>grep [选项] 搜索内容 搜索文件</code></li>
<li><code>-i</code>忽略大小写</li>
<li><code>-n</code>输出行号</li>
<li><code>-v</code>反向查找</li>
<li><code>--color=auto</code>搜索出的关键字用颜色标注</li>
</ul></li>
<li><p>find命令是完全匹配，如需模糊匹配需要使用通配符</p>
<p>grub是包含匹配。如果需要模糊匹配需要使用正则表达式</p>
<p>当对文件名使用通配符的时候最好使用双引号包起来</p></li>
<li><p>通配符表</p></li>
</ul>
<table>
<thead>
<tr class="header">
<th>通配符</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>?</td>
<td>匹配任意<strong>一个</strong>字符</td>
</tr>
<tr class="even">
<td>*</td>
<td>匹配任意多个(也可以是0个)字符</td>
</tr>
<tr class="odd">
<td>[]</td>
<td>匹配[]中的任意一个字符</td>
</tr>
<tr class="even">
<td>[-]</td>
<td>匹配[]中的任意一个字符，其中-代表范围，例如[a-z]</td>
</tr>
<tr class="odd">
<td>[^]</td>
<td>表示匹配不是[^]包裹的字符</td>
</tr>
</tbody>
</table>
<ul>
<li><p>正则表达式</p>
<table>
<thead>
<tr class="header">
<th>正则符</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>?</td>
<td>匹配前一个字符0/1次</td>
</tr>
<tr class="even">
<td>*</td>
<td>匹配前一个字符0/任意多次</td>
</tr>
<tr class="odd">
<td>[]</td>
<td>匹配[]中的任意一个字符</td>
</tr>
<tr class="even">
<td>[-]</td>
<td>匹配[]中的任意一个字符，其中-代表范围，例如[a-z]</td>
</tr>
<tr class="odd">
<td>[^]</td>
<td>匹配不是[^]包裹的字符</td>
</tr>
<tr class="even">
<td>^</td>
<td>匹配行首</td>
</tr>
<tr class="odd">
<td>$</td>
<td>匹配行尾</td>
</tr>
</tbody>
</table>
<p>其中<code>?</code>不属于标准正则而是扩展正则，需要使用<code>egrep</code>匹配</p></li>
</ul>
<h3 id="管道符">管道符</h3>
<ul>
<li>格式：<code>命令1 | 命令2</code></li>
<li>命令1的输出作为命令2的操作对象</li>
</ul>
<h3 id="别名与快捷命令">别名与快捷命令</h3>
<p>别名是就是为命令创建小名方便使用</p>
<ul>
<li>可以使用<code>alias</code>查看当前的所有别名</li>
<li>使用<code>alias  别名="原命令"</code>的方式<strong>临时</strong>定义别名，但是别名的优先级高于系统指令所以如果别名和原命令同名就会优先执行别名，原命令就无法执行了</li>
<li>可以修改环境变量自定义别名，但是别名应该是一个人的习惯而不是所有人的，所以应该设置为某用户的环境变量，修改<code>~/.bashrc</code>即可</li>
</ul>
<p>Bash下的常用快捷键有</p>
<table>
<thead>
<tr class="header">
<th>快捷键</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Tab</td>
<td>补全命令/文件</td>
</tr>
<tr class="even">
<td>Ctrl+A</td>
<td>将光标移动到命令的开头</td>
</tr>
<tr class="odd">
<td>Ctrl+E</td>
<td>将光标移动到命令的末尾</td>
</tr>
<tr class="even">
<td>Ctrl+C</td>
<td>强制结束命令</td>
</tr>
<tr class="odd">
<td>Ctrl+L</td>
<td>清屏</td>
</tr>
<tr class="even">
<td>Ctrl+U</td>
<td>剪切光标之前的命令</td>
</tr>
<tr class="odd">
<td>Ctrl+Y</td>
<td>粘贴Ctrl+U的命令</td>
</tr>
</tbody>
</table>
<h3 id="压缩与解压命令">压缩与解压命令</h3>
<p>压缩文件有很多种，不同的压缩方式有不同的命令</p>
<ul>
<li><code>zip</code>格式
<ul>
<li>压缩：<code>zip 目标压缩包.zip 原文件1 [原文件2...]</code></li>
<li>解压：<code>unzip 压缩包 [解压路径]</code>不写解压路径默认到当前目录</li>
<li>这个格式在linux中不常用，主要是为了兼容windows，是唯一一个可用的windows压缩格式</li>
</ul></li>
<li><code>gz</code>格式
<ul>
<li>压缩：<code>gzip 原文件1 [原文件2...]</code>
<ul>
<li>压缩会自动删除原文件，可以使用<code>-c</code>选项，他的原意是将压缩得到的二进制结果输出出来(实际上并不压缩)，可以结合管道使用<code>gzip -c 原文件 &gt;&gt; 压缩包.gz</code></li>
<li><code>-r</code>压缩目录，注意这个选项的意思，gz是不会将目录压缩成文件，而是将这个文件夹下的所有文件(包括子目录的)全部单个压缩，即<strong>gz不会打包</strong>，例如执行<code>gzip a b c d</code>就会得到a.gz,b.gz,c.gz,d.gz，如果不加<code>-r</code>gzip会直接提示访问到一个目录直接忽略</li>
</ul></li>
<li>解压：<code>gzip -d 压缩文件</code>或者<code>gunzip 压缩文件</code></li>
</ul></li>
<li><code>bz2</code>格式
<ul>
<li>压缩：<code>bzip2 [选项] 原文件</code>
<ul>
<li><code>-k</code>保留原文件</li>
<li><code>-v</code>显示详细信息</li>
<li>压缩命令<strong>不能打包</strong>，遇到目录直接报错</li>
</ul></li>
<li>解压：<code>bzip2 -d 压缩文件</code></li>
</ul></li>
<li><code>tar</code>格式
<ul>
<li>tar是用于打包的目录，<strong>只打包不压缩</strong></li>
<li>打包：<code>tar [选项] [-f 压缩包文件名] 源文件或目录</code>
<ul>
<li><code>-c</code>打包</li>
<li><code>-v</code>显示打包过程</li>
<li><code>-f</code>指定压缩包的文件名，用于给管理员识别是给打包文件</li>
<li>一般习惯写<code>-cvf</code></li>
</ul></li>
<li>解打包：<code>tar -xvf 打包文件</code></li>
</ul></li>
<li><code>tar.gz</code>/<code>tar.bz2</code>格式
<ul>
<li>压缩为<code>tar.gz</code>：<code>tar -zcvf 目标文件 原文件</code></li>
<li>解缩<code>tar.gz</code>：<code>tar -zxvf 压缩文件 [-C 解压位置] [要单独解压的文件]</code></li>
<li>查看<code>tar.gz</code>：<code>tar -ztvf 压缩文件</code></li>
<li>压缩为<code>tar.bz2</code>：<code>tar -jcvf 目标文件 原文件</code></li>
<li>解缩<code>tar.bz2</code>：<code>tar -jxvf 压缩文件 [-C 解压位置] [要单独解压的文件]</code></li>
<li>查看<code>tar.bz2</code>：<code>tar -jtvf 压缩文件</code></li>
</ul></li>
</ul>
<h3 id="开关机命令">开关机命令</h3>
<ul>
<li><code>sync</code>命令
<ul>
<li>用于数据同步，刷新文件系统缓冲区</li>
</ul></li>
<li><code>shutdown</code>命令
<ul>
<li>shutdown是最安全的</li>
<li><code>shutdown 选项 时间</code></li>
<li><code>-r</code>重启</li>
<li><code>-h</code>关机</li>
<li><code>-c</code>取消已经执行的shutdown命令</li>
<li>时间可以选<code>now</code>或者<code>7:10</code>这样的时间</li>
</ul></li>
<li><code>reboot</code>命令
<ul>
<li>重启，也是安全的</li>
</ul></li>
<li><code>halt</code>与<code>poweroff</code>
<ul>
<li>直接关机，都不安全</li>
</ul></li>
<li><code>init 0</code>与<code>init 6</code>
<ul>
<li>关机与重启，都不安全</li>
</ul></li>
</ul>
<h3 id="网络命令">网络命令</h3>
<ul>
<li><p>配置IP地址</p>
<ul>
<li><p>配置IP地址</p>
<ul>
<li><code>setup</code>目录图形化实现(redhat专有)</li>
<li><code>vim /etc/sysconfig/network-script/ifcfg-网卡名</code></li>
</ul></li>
<li><p>重启网络服务</p>
<ul>
<li><p><code>service network restart</code></p></li>
<li><p>如果发现某个网卡重启失败大概率是ip冲突</p></li>
<li><p>如果换了很多ip还是提示失败大概率是因为装系统的时候使用的是克隆的方式，导致UUID冲突，解决方法是删除mac与通过mac计算uuid的规则文件，然后重启，系统就会自动重新计算UUID</p>
<p><pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">vim</span> /etc/sysconfig/network-scripts/ifcfg-网卡名
<span class="token comment"># 删除 HWADDR=XX:XX:XX:XX:XX:XX 一行</span>
<span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">rm</span> -rf /etc/udev/rules.d/70-persistent-net.rules
<span class="token comment"># 删除规则文件</span>
<span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">reboot</span></code></pre></p></li>
</ul></li>
</ul></li>
<li><p><code>ifconfig</code>命令</p>
<ul>
<li><p>最主要的作用就是查IP，直接输入ifconfig即可</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ <span class="token function">ifconfig</span>
eth0      Link encap:Ethernet  HWaddr 08:00:27:45:12:CA  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
        <span class="token comment"># IP地址				  广播地址			 子网掩码</span>
          inet6 addr: fe80::a00:27ff:fe45:12ca/64 Scope:Link
        <span class="token comment"># IPv6地址</span>
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1316 errors:0 dropped:0 overruns:0 frame:0
        <span class="token comment"># 接受数据包的情况</span>
          TX packets:766 errors:0 dropped:0 overruns:0 carrier:0
        <span class="token comment"># 发送数据包的情况</span>
          collisions:0 txqueuelen:1000 
          RX bytes:109033 <span class="token punctuation">(</span><span class="token number">106.4</span> KiB<span class="token punctuation">)</span>  TX bytes:136348 <span class="token punctuation">(</span><span class="token number">133.1</span> KiB<span class="token punctuation">)</span></code></pre></li>
</ul></li>
<li><p><code>ping</code>命令</p>
<ul>
<li><code>ping [选项] IP</code>通过ICMP协议进行网络探测</li>
<li><code>-b 广播地址</code>对整个网段进行探测</li>
<li><code>-c 次数</code>指定ping的次数</li>
<li><code>-s 字节数</code>指定探测包的大小</li>
</ul></li>
<li><p><code>netstat</code>命令</p>
<ul>
<li><p>网络状态查看命令，可以看到本机的端口，也可以看到连接的客户端，centOS
7.x默认没有安装，需要使用则要安装<code>net-snmp</code>与<code>net-tools</code></p></li>
<li><p><code>-tuln</code>查询本机所有开启的端口</p>
<pre class="language-none"><code class="language-none">[liukairui@10 ~]$ netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address     Foreign Address             State 
tcp        0      0 0.0.0.0:40398     0.0.0.0:*                   LISTEN
协议   接受发送数据包队列  本机IP与端口号    远程主机与端口号              状态</code></pre></li>
<li><p><code>-tulnp</code>查询所有开启的端口与那个服务开启了这个端口</p></li>
<li><p><code>-rn</code>查询网关</p></li>
</ul></li>
<li><p><code>write</code>命令</p>
<ul>
<li><p>Linux是多用户的，一个系统可以多账户同时登陆，可以使用write进行用户之间的交流</p></li>
<li><p>用户登陆的时候有不同的终端，有<strong>本地文本</strong>终端是从TTY1-6
可以使用Ctrl+Alt+F1-6切换，还有图形界面TTY7，安装图形界面后按Ctrl+Alt+F7三秒切换，<strong>远程终端</strong>pts/0-255</p></li>
<li><p><code>write 用户名 回车之后填写要发送的内容</code></p>
<pre class="language-none"><code class="language-none">[root@10 ~]# w      # 使用w查看当前的用户
 20:39:11 up 23 min,  2 users,  load average: 0.00, 0.01, 0.04
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
liukairu pts&#x2F;0    10.0.2.2         20:19    2:03   0.01s  0.01s -bash
root     pts&#x2F;1    10.0.2.2         20:18    0.00s  0.06s  0.00s w
[root@10 ~]# write liukairui pts&#x2F;0    # 写出用户名和登陆的终端
hello </code></pre>
<p>按ctrl+d结束发送</p></li>
</ul></li>
<li><p><code>wall</code>命令</p>
<ul>
<li>想包括自己的所有人发送消息</li>
<li><code>wall 内容</code></li>
</ul></li>
<li><p>邮件命令</p>
<ul>
<li>发邮件：<code>mail 收件人</code>回车输入标题，回车输入内容</li>
<li>查邮件：<code>mail</code>未读邮件前显示N</li>
<li><code>-s</code>指定邮件标题</li>
</ul></li>
</ul>
<h3 id="系统痕迹命令">系统痕迹命令</h3>
<p>系统中有一些日志文件，为了防止用户修改，这些文件都是二进制文件无法使用编辑器打开，只能使用对应命令查询</p>
<ul>
<li><p><code>w</code>命令</p>
<ul>
<li><p>显示系统中目前正在登陆的用户信息，文件位于<code>/var/run/utmp</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># w</span>
 <span class="token number">21</span>:43:18 up  <span class="token number">1</span>:27,  <span class="token number">2</span> users,  load average: <span class="token number">0.00</span>, <span class="token number">0.01</span>, <span class="token number">0.04</span>
<span class="token comment"># 系统时间  系统开机时间 当前用户数目			前1min 5min 15min 的平均负载，一般不超过核心数</span>
<span class="token environment constant">USER</span>     TTY      FROM      LOGIN@   IDLE   JCPU          PCPU          WHAT
liukairu pts/0    <span class="token number">10.0</span>.2.2  <span class="token number">20</span>:19    <span class="token number">5</span>.00s  <span class="token number">0</span>.02s         <span class="token number">0</span>.02s         -bash
root     pts/1    <span class="token number">10.0</span>.2.2  <span class="token number">20</span>:18    <span class="token number">0</span>.00s  <span class="token number">0</span>.07s         <span class="token number">0</span>.00s         w
登陆用户   登陆终端  登陆IP     登陆时间  闲置时间 所有进程占CPU时 当前进程占CPU时 用户当前操作</code></pre></li>
</ul></li>
<li><p><code>who</code>命令</p>
<ul>
<li>与<code>w</code>效果相同但是内容简单</li>
</ul></li>
<li><p><code>last</code>命令</p>
<ul>
<li><p>查看系统所有登陆过的用户信息</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># last</span>
liukairu pts/0        <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">20</span>:19   still logged <span class="token keyword">in</span>   
root     pts/1        <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">20</span>:18   still logged <span class="token keyword">in</span>   
root     pts/0        <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">20</span>:16 - <span class="token number">20</span>:19  <span class="token punctuation">(</span>00:02<span class="token punctuation">)</span>    
<span class="token function">reboot</span>   system boot  <span class="token number">2.6</span>.32-642.el6.x Mon Feb  <span class="token number">1</span> <span class="token number">20</span>:15 - <span class="token number">21</span>:53  <span class="token punctuation">(</span>01:37<span class="token punctuation">)</span>    
root     pts/1        <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">12</span>:14 - down   <span class="token punctuation">(</span>00:17<span class="token punctuation">)</span>    
liukairu pts/0        <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">10</span>:49 - <span class="token number">12</span>:31  <span class="token punctuation">(</span>01:41<span class="token punctuation">)</span>    
liukairu pts/0        <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">10</span>:48 - <span class="token number">10</span>:49  <span class="token punctuation">(</span>00:00<span class="token punctuation">)</span>    
liukairu pts/0        <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">10</span>:08 - <span class="token number">10</span>:48  <span class="token punctuation">(</span>00:40<span class="token punctuation">)</span>    
<span class="token function">reboot</span>   system boot  <span class="token number">2.6</span>.32-642.el6.x Mon Feb  <span class="token number">1</span> 09:51 - <span class="token number">12</span>:31  <span class="token punctuation">(</span>02:39<span class="token punctuation">)</span>    </code></pre></li>
</ul></li>
<li><p><code>lastlog</code>命令</p>
<ul>
<li><p>查询所有用户的最后登陆时间</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># lastlog</span>
用户名           端口     来自             最后登陆时间
root             pts/1    <span class="token number">10.0</span>.2.2         一 <span class="token number">2</span>月  <span class="token number">1</span> <span class="token number">20</span>:18:56 +0800 <span class="token number">2021</span>
bin                                        **从未登录过**
daemon                                     **从未登录过**
adm                                        **从未登录过**
lp                                         **从未登录过**
<span class="token function">sync</span>                                       **从未登录过**
<span class="token function">shutdown</span>                                   **从未登录过**
<span class="token function">halt</span>                                       **从未登录过**
mail                                       **从未登录过**
uucp                                       **从未登录过**
operator                                   **从未登录过**
games                                      **从未登录过**
gopher                                     **从未登录过**
<span class="token function">ftp</span>                                        **从未登录过**
nobody                                     **从未登录过**
dbus                                       **从未登录过**
rpc                                        **从未登录过**
vcsa                                       **从未登录过**
abrt                                       **从未登录过**
rpcuser                                    **从未登录过**
nfsnobody                                  **从未登录过**
haldaemon                                  **从未登录过**
ntp                                        **从未登录过**
saslauth                                   **从未登录过**
postfix                                    **从未登录过**
sshd                                       **从未登录过**
tcpdump                                    **从未登录过**
oprofile                                   **从未登录过**
liukairui        pts/0    <span class="token number">10.0</span>.2.2         一 <span class="token number">2</span>月  <span class="token number">1</span> <span class="token number">20</span>:19:20 +0800 <span class="token number">2021</span></code></pre>
<p>注意，这个命令会列出所有的最近登陆记录，可以看到只有第一个和最后一个是实际存在的用户，其余用户是不能登陆的，也不是我们创建的，叫做伪用户，用来启动特殊的服务</p></li>
</ul></li>
<li><p><code>lastb</code>查看错误的登陆记录</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># lastb</span>
liukairu ssh:notty    <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">21</span>:58 - <span class="token number">21</span>:58  <span class="token punctuation">(</span>00:00<span class="token punctuation">)</span>    
liukairu ssh:notty    <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">21</span>:58 - <span class="token number">21</span>:58  <span class="token punctuation">(</span>00:00<span class="token punctuation">)</span>    
liukairu ssh:notty    <span class="token number">10.0</span>.2.2         Mon Feb  <span class="token number">1</span> <span class="token number">21</span>:58 - <span class="token number">21</span>:58  <span class="token punctuation">(</span>00:00<span class="token punctuation">)</span>    

btmp begins Mon Feb  <span class="token number">1</span> <span class="token number">21</span>:58:12 <span class="token number">2021</span></code></pre></li>
</ul>
<h3 id="挂载命令">挂载命令</h3>
<p>挂载是将一个存储设备的设备名与已经存在的空目录连起来的过程</p>
<ul>
<li><p><code>mount</code>挂载命令</p>
<ul>
<li><p><code>mount</code>直接使用可以查看当前所有挂载的设备</p>
<ul>
<li><code>-l</code>显示卷标</li>
<li><code>-t 文件系统</code>指定文件系统</li>
<li><code>-o</code>特殊选项，常用的就是remount来修改挂载的权限</li>
</ul></li>
<li><p><code>mount -a</code>检查自动挂载文件<code>etc/farab</code>，但是这个功能很弱</p>
<p>Linux支持设备自动挂载，但是光驱等设备不建议自动挂载，否则开机找不到光盘就开不了机，配置文件在<code>/etc/fstab</code>系统开机就会从这个文件读取</p></li>
<li><p>挂载一个光盘</p>
<ul>
<li><p>在CentOS 5.x-光盘是/dev/hdc/</p>
<p>在CentOS 6.x+光盘是/dev/sr0/</p>
<p>无论是什么系统，都有一个/dev/cdrom软链接到实际的光盘设备，例如
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ ll /dev/cdrom 
lrwxrwxrwx. <span class="token number">1</span> root root <span class="token number">3</span> <span class="token number">2</span>月   <span class="token number">1</span> <span class="token number">20</span>:15 /dev/cdrom -<span class="token operator">></span> sr0</code></pre></p></li>
<li><p>挂载命令</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># mkdir /mnt/cdrom</span>
<span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># mount -t iso9660 /dev/cdrom /mnt/cdrom/</span></code></pre>
<p>其中<code>-t iso9660</code>表示指定挂载文件系统为iso9660背下来即可，可以省略</p>
<p>但是可能会提示不识别文件系统或者找不到/dev/sr0这都是因为没放光盘</p>
<p>还可能会报错<code>mount: block device /dev/sr0 is write-protected, mounting read-only</code>说系统想挂载光盘为读写但是挂载成了读，因为大部分光盘是只读的，所以这恰好可以认为是光盘正确挂载了</p></li>
<li><p>卸载命令</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 cdrom<span class="token punctuation">]</span><span class="token comment"># cd ../</span>
<span class="token punctuation">[</span>root@10 mnt<span class="token punctuation">]</span><span class="token comment"># umount /dev/sr0</span></code></pre></li>
</ul></li>
<li><p>挂载U盘</p>
<ul>
<li><p>U盘与硬盘在Linux中共享设备名<code>sdx</code>所以U盘的设备名是动态的，例如有3个硬盘，那么U盘就是<code>sdd</code>所以在挂载之前<code>fdisk -l</code>查看所有的U盘</p></li>
<li><p>挂载命令</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">mkdir</span> /mnt/usb
<span class="token function">mount</span> -t vfat /dev/sdb4 /mnt/usb		<span class="token comment"># U盘是FAT32的写vfat</span></code></pre>
<p>注意，进入U盘之后ls可能会出现无法现实中文，修改方式是在挂载的时候指定编码</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">mount</span> -t vfat -o <span class="token assign-left variable">iocharset</span><span class="token operator">=</span>utf8 /dev/sdb4 /mnt/usb</code></pre>
<p>前提是Linux安装的时候选择了中文编码，同时tty支持中文</p></li>
<li><p>卸载命令</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">umount</span> /dev/sdb4</code></pre></li>
</ul></li>
</ul></li>
<li><p>挂载NTFS分区</p>
<p>windows下大多数使用的是NTFS分区，但是Linux不识别NTFS分区，可以通过修改实现<strong>只读</strong></p>
<ul>
<li><p>Linux的驱动加载顺序是</p>
<ul>
<li>系统加载必备的驱动会直接放在系统的内核中这这部分数量比较少</li>
<li>驱动以模块的形式放在硬盘中，存放在<code>/lib/modules/2.6.32-642.el6.x86_64/kernel/</code></li>
<li>驱动可以被Linux识别，但是Linux认为不常用，默认不加载，如果需要手动安装那么需要重新编译内核，NTFS属于这个范畴</li>
<li>部分硬件不被Linux识别，那么需要手动安装驱动，甚至是手写</li>
</ul></li>
<li><p>可以使用<code>ntfs-3G</code>插件手动安装，然后</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">mount</span> -t ntfs-3g /dev/sdb1 /mnt/win</code></pre></li>
</ul></li>
</ul>
<h2 id="vim编辑器">Vim编辑器</h2>
<p>基础内容略，见<a
href="www.liukairui.cc/Vim使用全指南(环境配置,插件推荐,美化)(C++,Python,MarkDown,R...)">Vim笔记</a></p>
<p>windows文件中的回车会在linux中显示为<code>^M$</code>，Linux中的回车会显示成<code>$</code>可以使用<code>dos2unix</code>，<code>unix2dos</code>实现相互转化</p>
<h2 id="软件包安装">软件包安装</h2>
<h3 id="软件包的分类">软件包的分类</h3>
<p>Windows与Linux的软件是不通用的，windows的软件是经过编译的二进制exe/msi文件，Linux中有软件有<strong>源码包</strong>和<strong>二进制包</strong>文件</p>
<ul>
<li>如果服务是给大量客户端使用的，建议使用源码包，因为源码包效率高</li>
<li>如果是给本机使用的建议使用RPM包</li>
</ul>
<h4 id="二进制包">二进制包</h4>
<p>二进制包的分类</p>
<ul>
<li>DPKG包：由Debian Linux开发的包管理机制，主要用于Debian和Ubuntu</li>
<li>RPM包：有RedHat开发的包管理系统，主要用于Fedora，CentOS,suSE</li>
</ul>
<p>RPM包依赖</p>
<ul>
<li><p>树形依赖：a-&gt;b-&gt;c</p></li>
<li><p>环形依赖：a-&gt;b-&gt;c-&gt;a</p></li>
<li><p>模块依赖(函数库依赖)，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># mount /dev/cdrom /mnt/cdrom/</span>
mount: block device /dev/sr0 is write-protected, mounting read-only
<span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># cd /mnt/cdrom/Packages/</span>
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh mysql-connector-odbc-5.1.5r1144-7.el6.x86_64.rpm </span>
warning: mysql-connector-odbc-5.1.5r1144-7.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
error: Failed dependencies:				<span class="token comment"># 错误：失败依赖性</span>
        libodbcinst.so.2<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span>64bit<span class="token punctuation">)</span> is needed by mysql-connector-odbc-5.1.5r1144-7.el6.x86_64
        unixODBC is needed by mysql-connector-odbc-5.1.5r1144-7.el6.x86_64</code></pre>
<p>可以看到，我们安装的是mysql-connector-odbc-5.1.5r1144-7.el6.x86_64.rpm但是报错提示的是这个包依赖于mysql-connector-odbc-5.1.5r1144-7.el6.x86_64这说明我们看反了...依赖的是libodbcinst.so.2()(64bit)可以看到他依赖的是一个.so.数字，是一个函数库，于是</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">rpm</span> -ivh libodbc<span class="token operator">&lt;</span>Tab<span class="token operator">></span></code></pre>
<p>于是tab发现并没有补全，这是因为压根就没有这个软件，我们缺少的是函数库，这个函数库只是某个软件包的一个函数库，需要先安装这个软件才能带出这个软件库，但是这个软件的名字我们不知道，可以去<a
target="_blank" rel="noopener" href="http://rpmfind.net/">http://rpmfind.net/</a>查询，打开链接输入<code>libodbcinst.so.2</code>看到第三列是系统名字，找到我们的CentOS(如果发现一个CentOS都没有，可以刷新一下...)对应的一行，右边一列就是那个需要安装的软件包</p></li>
</ul>
<p>RedHat也认为依赖难以解决于是在手动安装包的基础上开发了yum安装的方式(注意没有yum包，只有源码包或者rpm包)，yum包安装软件的时候会自动链接RedHat的服务器获取依赖信息，下载依赖文件</p>
<h3 id="二进制rpm包命令">二进制RPM包命令</h3>
<h4 id="rpm包的命名规则">RPM包的命名规则</h4>
<pre class="language-none"><code class="language-none">httpd-2.2.15-53.el6.centos.x86_64.rpm </code></pre>
<p>上面是Apache的软件包，有几个部分组成</p>
<ul>
<li><p>httpd：软件包名，注意这是软件包名/进程名，可以不叫Apach</p></li>
<li><p>2.2.15：软件版本</p></li>
<li><p>53：发布次数</p></li>
<li><p>e16：软件发行商。e16是RedHat发布的适合于RHEL6.x下使用</p></li>
<li><p>x86_64：适合的硬件平台，RPM包可以在不同硬件平台安装选择适合不同CPU的软件版本所以出现了有</p>
<ul>
<li><code>i386</code>适合386以上平台使用</li>
<li><code>i686</code>适合奔腾II以上计算机安装</li>
<li><code>x86_64</code>64位CPU均可安装</li>
<li><code>noarch</code>没有硬件限制</li>
</ul>
<p>等文件名</p></li>
<li><p>rpm：扩展名，用来帮助管理员区分</p></li>
</ul>
<p>以上是包全名，还有包名就是第一部分的软件包名，如果是要操作没有安装的软件包就要使用包全名与绝对路径，如果操作已经安装的软件包可以直接使用包名，系统会自动搜索rpm数据库(<code>/var/lib/rpm</code>)找到路径</p>
<h4 id="rpm包的安装">RPM包的安装</h4>
<ul>
<li><p>安装命令</p>
<ul>
<li><code>rpm -ivh 包全名</code></li>
<li><code>-i</code>安装</li>
<li><code>-v</code>显示详细信息</li>
<li><code>-h</code>打印#显示进度</li>
<li><code>--perfix=</code>指定路径，如果不指定，软件会安装在开发者指定的安装位置，不建议修改路径，否则不易维护与卸载</li>
<li><code>--nodeps</code>不检测依赖安装(但是如果不检测依赖的软件基本不能用，不建议使用)</li>
<li><code>--replacefiles</code>如果安装的时候部分文件已存在就会报错，使用这个选项可以强制覆盖</li>
<li><code>--force</code>强制安装，相当于<code>--replacefiles --nodeps</code></li>
<li><code>--test</code>测试安装，不实际安装，只是检测依赖</li>
</ul></li>
<li><p>使用RPM安装Apache</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh httpd-</span>
httpd-2.2.15-53.el6.centos.x86_64.rpm         httpd-manual-2.2.15-53.el6.centos.noarch.rpm
httpd-devel-2.2.15-53.el6.centos.x86_64.rpm   httpd-tools-2.2.15-53.el6.centos.x86_64.rpm
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh httpd-2.2.15-53.el6.centos.x86_64.rpm </span>
warning: httpd-2.2.15-53.el6.centos.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
error: Failed dependencies:
        apr-util-ldap is needed by httpd-2.2.15-53.el6.centos.x86_64
        httpd-tools <span class="token operator">=</span> <span class="token number">2.2</span>.15-53.el6.centos is needed by httpd-2.2.15-53.el6.centos.x86_64
        libapr-1.so.0<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span>64bit<span class="token punctuation">)</span> is needed by httpd-2.2.15-53.el6.centos.x86_64
        libaprutil-1.so.0<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span>64bit<span class="token punctuation">)</span> is needed by httpd-2.2.15-53.el6.centos.x86_64
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh apr-devel-1.3.9-5.el6_2.x86_64.rpm </span>
warning: apr-devel-1.3.9-5.el6_2.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
error: Failed dependencies:
        apr <span class="token operator">=</span> <span class="token number">1.3</span>.9-5.el6_2 is needed by apr-devel-1.3.9-5.el6_2.x86_64
        libapr-1.so.0<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span>64bit<span class="token punctuation">)</span> is needed by apr-devel-1.3.9-5.el6_2.x86_64
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh apr-</span>
apr-1.3.9-5.el6_2.x86_64.rpm               apr-util-devel-1.3.9-3.el6_0.1.x86_64.rpm
apr-devel-1.3.9-5.el6_2.x86_64.rpm         apr-util-ldap-1.3.9-3.el6_0.1.x86_64.rpm
apr-util-1.3.9-3.el6_0.1.x86_64.rpm        
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh apr-1.3.9-5.el6_2.x86_64.rpm </span>
warning: apr-1.3.9-5.el6_2.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing<span class="token punctuation">..</span>.                <span class="token comment">########################################### [100%]</span>
   <span class="token number">1</span>:apr                    <span class="token comment">########################################### [100%]</span>
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh apr-devel-1.3.9-5.el6_2.x86_64.rpm </span>
warning: apr-devel-1.3.9-5.el6_2.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing<span class="token punctuation">..</span>.                <span class="token comment">########################################### [100%]</span>
   <span class="token number">1</span>:apr-devel              <span class="token comment">########################################### [100%]</span>
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh httpd-tools-2.2.15-53.el6.centos.x86_64.rpm </span>
warning: httpd-tools-2.2.15-53.el6.centos.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
error: Failed dependencies:
        libaprutil-1.so.0<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span>64bit<span class="token punctuation">)</span> is needed by httpd-tools-2.2.15-53.el6.centos.x86_64
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh apr-util-</span>
apr-util-1.3.9-3.el6_0.1.x86_64.rpm        apr-util-ldap-1.3.9-3.el6_0.1.x86_64.rpm
apr-util-devel-1.3.9-3.el6_0.1.x86_64.rpm  
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh apr-util-1.3.9-3.el6_0.1.x86_64.rpm </span>
<span class="token punctuation">]</span>warning: apr-util-1.3.9-3.el6_0.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Preparing<span class="token punctuation">..</span>.                <span class="token comment">########################################### [100%]</span>
   <span class="token number">1</span>:apr-util               <span class="token comment">########################################### [100%]</span>
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh httpd-tools-2.2.15-53.el6.centos.x86_64.rpm </span>
warning: httpd-tools-2.2.15-53.el6.centos.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing<span class="token punctuation">..</span>.                <span class="token comment">########################################### [100%]</span>
   <span class="token number">1</span>:httpd-tools            <span class="token comment">########################################### [100%]</span>
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh httpd-2.2.15-53.el6.centos.x86_64.rpm </span>
warning: httpd-2.2.15-53.el6.centos.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
error: Failed dependencies:
        apr-util-ldap is needed by httpd-2.2.15-53.el6.centos.x86_64
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh apr-util-ldap-1.3.9-3.el6_0.1.x86_64.rpm </span>
warning: apr-util-ldap-1.3.9-3.el6_0.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Preparing<span class="token punctuation">..</span>.                <span class="token comment">########################################### [100%]</span>
   <span class="token number">1</span>:apr-util-ldap          <span class="token comment">########################################### [100%]</span>
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh httpd-</span>
httpd-2.2.15-53.el6.centos.x86_64.rpm         httpd-manual-2.2.15-53.el6.centos.noarch.rpm
httpd-devel-2.2.15-53.el6.centos.x86_64.rpm   httpd-tools-2.2.15-53.el6.centos.x86_64.rpm
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh httpd-2.2.15-53.el6.centos.x86_64.rpm </span>
warning: httpd-2.2.15-53.el6.centos.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing<span class="token punctuation">..</span>.                <span class="token comment">########################################### [100%]</span>
   <span class="token number">1</span>:httpd                  <span class="token comment">########################################### [100%]</span>
<span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh httpd-manual-2.2.15-53.el6.centos.noarch.rpm </span>
warning: httpd-manual-2.2.15-53.el6.centos.noarch.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing<span class="token punctuation">..</span>.                <span class="token comment">########################################### [100%]</span>
   <span class="token number">1</span>:httpd-manual           <span class="token comment">########################################### [100%]</span></code></pre>
<p>启动Apache服务</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># service httpd restart</span>
停止 httpd：                                               <span class="token punctuation">[</span>确定<span class="token punctuation">]</span>
正在启动 httpd：                                           <span class="token punctuation">[</span>确定<span class="token punctuation">]</span></code></pre>
<p>也可以是</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 Packages<span class="token punctuation">]</span><span class="token comment"># /etc/rc.d/init.d/httpd restart</span>
停止 httpd：                                               <span class="token punctuation">[</span>确定<span class="token punctuation">]</span>
正在启动 httpd：                                           <span class="token punctuation">[</span>确定<span class="token punctuation">]</span></code></pre>
<p>实际上service就是搜索的<code>/etc/rc.d/init.d/httpd</code></p>
<p><code>netstat -tulnp</code>看到httpd跑在80上</p>
<p><code>setup</code>关闭防火墙</p>
<p><code>ifconfig</code>看ip</p>
<p>网页的位置在<code>/var/www/html</code></p>
<p>配置文件在<code>/etc/httpd/conf/httpd.conf</code></p></li>
</ul>
<h4 id="rpm包升级">RPM包升级</h4>
<ul>
<li><code>rpm -Uvh 全名</code>：升级安装，没装会直接装，装了会升级</li>
<li><code>rpm -Fvh 全名</code>：升级安装，只升级不安装</li>
</ul>
<h4 id="rpm包卸载">RPM包卸载</h4>
<ul>
<li><code>rpm -e 包名</code></li>
<li><code>--nodeps</code>不检查依赖 ，生产不得使用</li>
<li>卸载的时候会提示程序有某个依赖还没有卸载，需要卸载依赖才可以卸载他，例如<code>hpptd-&gt;httpd-tools</code>那么卸载http的时候就会提示还没卸载httpd-tools，比较麻烦，但是不能用yum替代，yum在卸载的时候会自动处理依赖，但是例如有依赖关系<code>systemXXX-&gt;httpd-&gt;httptool</code>某个系统工具依赖httpd,当卸载httpd时候yum认为http-tool没有httpd也可以独立存在没有卸载的必要，但是systemXXX依赖于httpd,没了httpd的话systemXXX也没有必要，于是连带卸载了，但是systemXXX很可能是系统软件直接删了管理员又不知道，系统直接崩</li>
</ul>
<h4 id="rpm包的查询">RPM包的查询</h4>
<ul>
<li><code>rpm -q 包名</code>可以查询某个包是否安装，注意yum的查询与之不同，yum的查询是查询RedHat也没有这个包</li>
<li><code>rpm -qa</code>可以系统中所有的包</li>
<li><code>rpm -qi 包名</code>可以查询某个包的详细信息</li>
<li><code>rpm -qip 包全名</code>可以查询某<strong>未安装</strong>包的详细信息</li>
<li><code>rpm -ql 包名</code>可以查询某包的文件安装位置</li>
<li><code>rpm -qlp 包全名</code>可以查询某<strong>未安装</strong>包的文件安装位置</li>
<li><code>rpm -qf 某文件名</code>可以反查这个文件是那个包的文件</li>
<li><code>rpm -qR 包名</code>查询软件的所有依赖包(注意不管装没装都写)</li>
</ul>
<h4 id="rpm包验证">RPM包验证</h4>
<ul>
<li><code>rpm -Va</code>验证本机所有已经安装的软件包</li>
<li><code>rpm -V 包名</code>验证某个已经安装的软件包</li>
<li><code>rpm -Vf 文件</code>验证某个软件包的文件</li>
</ul>
<p>验证的机制是检查文件与初始安装时发生的变化，例如修改httpd的conf文件</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># rpm -V httpd</span>
S.5<span class="token punctuation">..</span><span class="token punctuation">..</span>T.  c /etc/httpd/conf/httpd.conf</code></pre>
<p>系统会现实那个包发生了变化，变化的信息卸载前面，分别是</p>
<ul>
<li><code>S</code>文件大小发生变化</li>
<li><code>M</code>文件权限变化</li>
<li><code>5</code>文件MD5发生变化</li>
<li><code>D</code>设备主从代码发生改变</li>
<li><code>L</code>文件路径变化</li>
<li><code>U</code>文件所有者变化</li>
<li><code>G</code>文件所属组变化</li>
<li><code>T</code>文件的修改时间变化</li>
<li><code>c</code>这个文件是这个软件的配置文件</li>
<li><code>d</code>这个文件是这个软件的普通文件</li>
<li><code>g</code>这个文件是这个软件的鬼文件(不是RPM包的文件)</li>
<li><code>l</code>这个文件是这个软件的授权文件</li>
<li><code>r</code>这个文件是这个软件的描述文件</li>
</ul>
<p>这个RPM校验是与RPM记录的原始值进行对比，为了保证原始值有个数字证书的概念</p>
<h4 id="数字证书">数字证书</h4>
<p>数字证书，又称数字签名，由软件开发商直接发布。Linux
系统安装数字证书后，若 RPM
包做了修改，此包携带的数字证书也会改变，将无法与系统成功匹配，软件无法安装。</p>
<p>可以将数字证书想象成自己的签名，是不能被模仿的（厂商的数字证书是唯一的），只有我认可的文件才会签名（只要是厂商发布的软件，都符合数字证书验证）；如果我的文件被人修改了，那么我的签名就会变得不同（如果软件改变，数字证书就会改变，从而通不过验证。当然，现实中人的手工签名不会直接改变，所以数字证书比手工签名还要可靠）。</p>
<p>数字证书是为了保证RPM校验文件没有问题，具有如下特点</p>
<ul>
<li>必须找到原厂的公钥才可以安装</li>
<li>再安装PRM包的时候会提取RPM的证书与本机原厂证书对比验证</li>
<li>验证通过安装，不通过就报错</li>
</ul>
<p>数字证书的位置<code>/mnt/cdrom/RPM-GPG-KEY-CentOS-6</code>与<code>/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6</code></p>
<ul>
<li><p>安装数字证书</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># rpm --import /efc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6</span></code></pre></li>
<li><p>验证数字证书</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># rpm -qa | grep gpg-pubkey</span></code></pre></li>
</ul>
<h4 id="rpm包的文件提取命令">RPM包的文件提取命令</h4>
<p>在修改软件配置文件但是想恢复的时候会发现覆盖安装只会安装被删除的文件，但是不覆盖存在的文件，所以需要从rpm包中提取文件</p>
<ul>
<li><p>cpio命令</p>
<p>这个命令本身是用来做复制备份还原的，但是用它备份还原极其不方便，不推荐进行备份还原</p>
<p><strong>备份</strong></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">cpio -o<span class="token punctuation">[</span>vcB<span class="token punctuation">]</span> <span class="token operator">></span> <span class="token punctuation">[</span>文件<span class="token operator">|</span>设备<span class="token punctuation">]</span></code></pre>
<ul>
<li><code>-o</code>备份模式</li>
<li><code>-v</code>显示备份过程</li>
<li><code>-c</code>使用portable formate存储方式</li>
<li><code>-B</code>设定输入输出块为5120Bytes而不是5120butes</li>
</ul>
<p><strong>还原</strong></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">cpio -i<span class="token punctuation">[</span>vcdu<span class="token punctuation">]</span> <span class="token operator">&lt;</span> <span class="token punctuation">[</span>文件<span class="token operator">|</span>设备<span class="token punctuation">]</span></code></pre>
<ul>
<li><code>-i</code>还原模式</li>
<li><code>-v</code>显示还原过程</li>
<li><code>-c</code>使用portable formate存储方式</li>
<li><code>-d</code>还原时自动新建目录</li>
<li><code>-u</code>使用较新的文件覆盖旧文件</li>
</ul>
<p><strong>备份</strong></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">cpio -o<span class="token punctuation">[</span>vcB<span class="token punctuation">]</span> <span class="token operator">></span> <span class="token punctuation">[</span>文件<span class="token operator">|</span>设备<span class="token punctuation">]</span></code></pre>
<ul>
<li><code>-o</code>备份模式</li>
<li><code>-v</code>现实备份过程</li>
<li><code>-c</code>使用portable formate存储方式</li>
<li><code>-B</code>设定输入输出块为5120Bytes而不是5120butes</li>
</ul>
<p>举例备份还原<code>/etc</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">find</span> /etc -print <span class="token operator">|</span> cpio -ocvB <span class="token operator">></span> /root/etc.cpio	<span class="token comment"># 必须用find与管道找源文件用>方式写入</span>
cpio -idvcu <span class="token operator">&lt;</span> /root/etc.cpio				   <span class="token comment"># 必须这么恢复</span></code></pre>
<p><strong>提取RMP中文件</strong></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">rpm2cpio 包全名 <span class="token operator">|</span> cpio -idv ./文件的绝对路径</code></pre>
<p>注意，一定要写那个./文件的绝对路径，否则自动覆盖文件，写上之后会拷贝到当前目录，至于那个文件绝对路径只需要写成实际目录地址，例如提取Apach</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># rpm2cpio /mnt/cdrom/Packages/httpd-2.2.15-53.el6.centos.x86_64.rpm | cpio -idv ./etc/httpd/conf/httpd.conf </span>
./etc/httpd/conf/httpd.conf
<span class="token number">6222</span> 块
<span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># ll ./etc/httpd/conf/httpd.conf </span>
-rw-r--r--. <span class="token number">1</span> root root <span class="token number">34419</span> <span class="token number">2</span>月   <span class="token number">2</span> <span class="token number">23</span>:43 ./etc/httpd/conf/httpd.conf</code></pre></li>
</ul>
<h4 id="rpm包在线安装命令yum">RPM包在线安装命令yum</h4>
<ul>
<li><p>yum源的文件解析</p>
<p>yum源配置文件在<code>/etc/yum.repos.d</code>目录，扩展名是.repo，也就是说
yum源配置文件只要是.repo就会生效</p>
<p><pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># ls /etc/yum.repos.d/</span>
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-fasttrack.repo  CentOS-Media.repo  CentOS-Vault.repo</code></pre></p>
<p>默认有5个源，以此是是默认的基础的网络yum，调试的，快速的，光盘的，虚拟的，可以尝试打开base文件</p>
<p><pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>base<span class="token punctuation">]</span>											  <span class="token comment"># 容器名</span>
<span class="token assign-left variable">name</span><span class="token operator">=</span>CentOS-<span class="token variable">$releasever</span> - Base						<span class="token comment"># 容器描述</span>
<span class="token assign-left variable">mirrorlist</span><span class="token operator">=</span>http://mirrorlist.centos.org/?release<span class="token operator">=</span><span class="token variable">$releasever</span><span class="token operator">&amp;</span><span class="token assign-left variable">arch</span><span class="token operator">=</span><span class="token variable">$basearch</span><span class="token operator">&amp;</span><span class="token assign-left variable">repo</span><span class="token operator">=</span>os<span class="token operator">&amp;</span><span class="token assign-left variable">infra</span><span class="token operator">=</span><span class="token variable">$infra</span>
<span class="token comment">#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/</span>
<span class="token comment"># 镜像地址与原网址，只保留一个生效</span>
<span class="token assign-left variable">gpgcheck</span><span class="token operator">=</span><span class="token number">1</span>										  <span class="token comment"># 验证证书</span>
<span class="token assign-left variable">gpgkey</span><span class="token operator">=</span>file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6   <span class="token comment"># 证书位置</span>
<span class="token assign-left variable">enabled</span><span class="token operator">=</span><span class="token number">0</span>										  <span class="token comment"># 是否启用</span>
<span class="token punctuation">..</span>.</code></pre></p>
<p>网络源在国外很慢可以配置光盘源，方式是将其他的源修改为<code>.repo.bak</code>，将光盘源enable，修改地址到光盘挂载地址然后注释后面两个不用的地址</p></li>
<li><p>yum命令</p>
<p>注意在yum中不区分包名和包全名，全部使用包名即可</p>
<ul>
<li>查询
<ul>
<li>查询源中的所有包<code>yum list</code></li>
<li>查询源中的某个包<code>yum list 包名</code></li>
<li>模糊查询包<code>yum search 关键字</code></li>
</ul></li>
<li>安装
<ul>
<li><code>yum -y install 包名</code>其中<code>-y</code>是自动回答yes</li>
</ul></li>
<li>升级
<ul>
<li><code>yum -y update 包名</code>其中<code>-y</code>是自动回答yes</li>
<li><code>yum -y update</code>升级所有软件包括内核</li>
</ul></li>
<li>卸载
<ul>
<li><code>yum remove 包名</code>注意依赖关系，不建议使用</li>
</ul></li>
</ul></li>
<li><p>yum组管理</p>
<p>就像在安装的时候选择BasicSever一样可以安装一系列软件</p>
<ul>
<li>列出所有组：<code>rpm grouplist</code></li>
<li>查询一个组包括的软件包 <code>yum groupinfo 组</code></li>
<li>安装一个组<code>yum groupinstall 组</code></li>
<li>卸载一个组<code>yum grouremove 组</code></li>
</ul></li>
</ul>
<h3 id="源码包的安装">源码包的安装</h3>
<ul>
<li><p>源码包必须指定安装位置，否则会到处乱装，还不知道去哪里卸载</p></li>
<li><p>尝试安装apache-2.2.7</p>
<p>版本比较老，但是高版本CentOS6跑不起来，可以在此处下载<a
target="_blank" rel="noopener" href="http://archive.apache.org/dist/httpd/">http://archive.apache.org/dist/httpd/</a>然后搜索即可，注意从<a
target="_blank" rel="noopener" href="https://github.com/apache/httpd/releases/tag/2.2.9">https://github.com/apache/httpd/releases/tag/2.2.9</a>
下载的缺少文件不要使用，然后使用SCP传输到虚拟机(如果使用的是VirtualBox虚拟机可以使用<code>scp -P 9000 -r ./download/httpd-2.2.9.tar.bz2  liukairui@127.0.0.1:/home/liukairui</code>其中-P后面是我们之前配置的转发端口，没有-P则会自动连接22口会显示无法连接)</p>
<ul>
<li><p>解压</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># tar -jxvf httpd-2.2.9.tar.bz2 </span>
<span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># cd httpd-2.2.9</span></code></pre></li>
<li><p><code>./configure</code></p>
<p><code>configure</code>的作用是</p>
<ul>
<li>检查系统条件是否符合要求</li>
<li>自定义需要安装的功能，可以查询<code>./configure --help</code>查看</li>
<li>将系统环境检测好写入makefile</li>
</ul>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 httpd-2.2.9<span class="token punctuation">]</span><span class="token comment"># ./configure --prefix=/usr/local/apache2</span></code></pre></li>
<li><p>安装<code>gcc</code></p></li>
<li><p><code>make</code></p></li>
</ul>
<p>以上两条命令只是进行编译而没有安装，不会安装到<code>/usr/local/http2</code>所以如果报错只需要<code>make clean</code>清空文件即可，不需要删除<code>usr/local/http2</code></p>
<ul>
<li><code>make install</code></li>
</ul>
<p>如果install报错需要<code>make clean</code>删除<code>/usr/local/http2</code></p>
<ul>
<li>验证安装成功<code>ps aux | grep httpd</code></li>
</ul>
<p>如果是使用VirtualBox可以将网络设置为桥接网卡，其他默认，开机后<code>ifconfig</code>看到ip地址，之后ssh就可以不加-P端口了，同时在外面浏览器直接看ip可以看到网页正常工作</p></li>
<li><p>打补丁</p>
<p>生成一个测试补丁</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># mkdir ./test			# 创建一个测试目录</span>
<span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># cd test/				# 进入目录</span>
<span class="token punctuation">[</span>root@localhost test<span class="token punctuation">]</span><span class="token comment"># echo AAA >> old		# 创建老文件</span>
<span class="token punctuation">[</span>root@localhost test<span class="token punctuation">]</span><span class="token comment"># echo ABA >> new		# 创建新文件</span>
<span class="token punctuation">[</span>root@localhost test<span class="token punctuation">]</span><span class="token comment"># diff -Naur /root/test/old /root/test/new > txt.patch	# 生成补丁必须绝对路径</span></code></pre>
<p>看补丁</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost test<span class="token punctuation">]</span><span class="token comment"># cat txt.patch </span>
--- /root/test/old      <span class="token number">2021</span>-02-03 <span class="token number">23</span>:08:08.635990638 +0800
+++ /root/test/new      <span class="token number">2021</span>-02-03 <span class="token number">23</span>:08:18.253998956 +0800
@@ -1 +1 @@
-AAA
+ABA</code></pre>
<p>可以看到，这个补丁文件详细的记录了文件的绝对路径和修改信息</p>
<p>打补丁</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost test<span class="token punctuation">]</span><span class="token comment"># patch -p3 &lt; txt.patch </span>
patching <span class="token function">file</span> old</code></pre>
<p><code>-p3</code>的意思就是将当前pwd的位置退出几层，因为补丁中已经有了路径，如果不写patch会认为是`/root/test/root/test/所以我们要往出退2层写<strong>p3</strong>，p1是当前目录，p2是退一层，p3是退两层</p></li>
</ul>
<h4 id="脚本包安装">脚本包安装</h4>
<p>可能某个包需要大量的rpm和源码包，十分复杂，可以把安装目录写入shell脚本，以后只需要一条目录回车就可以</p>
<ul>
<li><p>使用脚本包安装Webmin</p>
<p>Webmin是一个基于Web的Linux系统管理界面</p>
<ul>
<li>解压</li>
<li><code>./install.sh</code></li>
</ul></li>
</ul>
<h2 id="用户管理">用户管理</h2>
<h3 id="用户相关文件">用户相关文件</h3>
<ul>
<li><p><code>/tec/passwd</code>用户信息文件</p>
<p>打开以后看到很多行，一行是一个用户，但是都不是自己添加的，是系统为了启动服务添加的，不能动，一行都是如下形式，分别是</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">root:x:0:0:root:/root:/bin/bash</code></pre>
<ul>
<li>用户名</li>
<li>密码位，x代表有密码，具体的密码在<code>/etc/shadow</code>,只有有x标记系统才会去shadow里面找密码</li>
<li>用户ID
<ul>
<li>0：超级用户UID，只要一个用户UID是0，就是超级用户，所以实际上是可以将一个普通用户升级成超级用户，但是不建议，如果这样的话，用提升后的用户执行<code>whoami</code>得到的是root,因为UID一样</li>
<li>1-499：系统用户(伪用户)</li>
<li>500-60000：普通用户</li>
</ul></li>
<li>用户的初始组ID，其中组ID与组名的对应关系在<code>/etc/group</code></li>
<li>简单解释用户是做什么的(可空)</li>
<li>用户的家目录</li>
<li>登陆之后执行的shell</li>
</ul></li>
<li><p><code>/etc/shadow</code> 影子文件</p>
<p>一共9列，分别是</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">root:<span class="token variable">$6</span><span class="token variable">$lDwFrlZqSZ</span>/jd7Yu<span class="token variable">$MSOi</span>/4Tka85amFZyV4mWO0swbLe0zUnuU28bo/v/p2uOs/sCyGFXMAiyJJBen5hxUZ9kn2yA.k6hcZmFJVR650:18655:0:99999:7:::</code></pre>
<ul>
<li>用户名</li>
<li>加密后的密码(SHA512)，<code>*</code>或者<code>!!</code>代表他们是没有密码的，如果我想让一个用户临时无法登陆，约定俗成的是在加密串前面加<code>!</code></li>
<li>上一次设置密码的时间戳</li>
<li>最小的修改密码间隔时间(天)</li>
<li>密码有效期(天)</li>
<li>密码到期前的警告时间</li>
<li>密码到期后的款先天数(默认是-1，表示永久有效)</li>
<li>密码的失效时间</li>
<li>保留列，目前没有意义</li>
</ul></li>
<li><p><code>/etc/group</code>组信息文件</p>
<p>只有四列</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">mail:x:12:mail,postfix</code></pre>
<ul>
<li>组名</li>
<li>组密码位(在<code>/etc/gshadow</code>)可以给组设置密码，但是不建议，组密码是用来做组管理员的，在默认情况下只有root可以修改用户组，现在可以让root设置组密码，之后普通用户使用组密码对一个组进行修改用户，但是不常用，不建议</li>
<li>组ID</li>
<li>组的附加用户</li>
</ul>
<p>初始组和附加组</p>
<ul>
<li>初始组：每个用户只能有一个，一般就是与用户名同名的组</li>
<li>附加组：用户可以有多个附加组，将用户加入组就是加入附加组</li>
</ul></li>
<li><p><code>/home/[username]/</code>用户家目录</p></li>
<li><p><code>/var/spool/mail/[username]/</code>是用户邮箱文件</p></li>
<li><p><code>/etc/skel</code>用户目录模板，当新建一个用户的时候用户文件夹下会自动创建的文件就是拷贝至此</p></li>
</ul>
<h3 id="用户管理命令">用户管理命令</h3>
<ul>
<li><p><code>useradd</code>命令</p>
<ul>
<li><p><code>-u XXX</code> 指定用户UID为XXX</p></li>
<li><p><code>-g</code> 指定初始组</p></li>
<li><p><code>-G</code> 指定附加组</p></li>
<li><p><code>-c</code> 添加说明</p></li>
<li><p><code>-d</code> 指定目录</p></li>
<li><p>以上选项基本不用，不建议使用，最常用的还是<code>useradd [username]</code>，默认选项的控制文件在<code>/etc/default/useradd</code>和<code>/etc/login.defs</code></p></li>
<li><p><code>/etc/default/useradd</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># useradd defaults file</span>
<span class="token assign-left variable">GROUP</span><span class="token operator">=</span><span class="token number">100</span>				<span class="token comment"># 添加用户的默认组id(但是目前不采用)</span>
<span class="token assign-left variable"><span class="token environment constant">HOME</span></span><span class="token operator">=</span>/home				<span class="token comment"># 默认家位置</span>
<span class="token assign-left variable">INACTIVE</span><span class="token operator">=</span>-1				<span class="token comment"># 密码过期的宽限天数(-1代表用不过期)</span>
<span class="token assign-left variable">EXPIRE</span><span class="token operator">=</span>					<span class="token comment"># 密码的失效时间 </span>
<span class="token assign-left variable"><span class="token environment constant">SHELL</span></span><span class="token operator">=</span>/bin/bash			 <span class="token comment"># shell目录</span>
<span class="token assign-left variable">SKEL</span><span class="token operator">=</span>/etc/skel			 <span class="token comment"># 模板目录</span>
<span class="token assign-left variable">CREATE_MAIL_SPOOL</span><span class="token operator">=</span>yes	  <span class="token comment"># 默认创建邮箱</span></code></pre></li>
<li><p><code>/etc/login.defs</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">MAIL_DIR	/var/spool/mail			<span class="token comment"># 邮箱地址</span>
PASS_MAX_DAYS	<span class="token number">99999</span>				<span class="token comment"># 密码有效期</span>
PASS_MIN_DAYS	<span class="token number">0</span>					<span class="token comment"># 改密码最小间隔</span>
PASS_MIN_LEN    <span class="token number">5</span>					<span class="token comment"># 密码最小长度(目前已经无效)</span>
PASS_WARN_AGE   <span class="token number">7</span>					<span class="token comment"># 到期警告天数</span>
UID_MIN                   <span class="token number">500</span>		 <span class="token comment"># UID 范围</span>
UID_MAX                 <span class="token number">60000</span>
GID_MIN                   <span class="token number">500</span>		 <span class="token comment"># GID 范围</span>
GID_MAX                 <span class="token number">60000</span>
CREATE_HOME     <span class="token function">yes</span>					<span class="token comment"># 自动建立家目录</span>
UMASK           077					<span class="token comment"># 家目录权限</span>
USERGROUPS_ENAB <span class="token function">yes</span>					<span class="token comment"># 使用userdel删除用户的时候是否删除家目录</span>
ENCRYPT_METHOD SHA512				<span class="token comment"># 密码存储加密方式</span>
</code></pre></li>
</ul></li>
<li><p><code>passwd</code> 命令</p>
<ul>
<li><p>普通用户修改自己密码直接<code>passwd</code>且必须符合密码原则，root使用<code>passwd [username]</code>修改别人密码</p></li>
<li><p><code>passwd -l [username]</code>可以将用户锁起来，原理就是在shadow里面用户密码前面加<code>!!</code></p></li>
<li><p><code>passwd -u [username]</code>可以将用户解锁，原理就是在shadow里面用户密码前面去掉<code>!!</code></p></li>
<li><p><code>passwd --stdin</code>
用于管道设置密码，例如<code>echo "123" | passwd --stdin [username]</code>这样就不会说什么重复输入密码</p></li>
<li><p><code>chage -d 0 [username]</code>将用户上次修改日期时间戳设为0(shadow的第三列)，这样系统认为是时间戳异常，在用户登陆后会要求修改密码</p></li>
</ul></li>
<li><p><code>usermod</code>命令</p>
<ul>
<li><code>-c</code>修改用户帐号的备注文字。 　-
<code>-d</code>修改用户登入时的目录。 　-
<code>-e</code>修改帐号的有效期限。 　-
<code>-f</code>修改在密码过期后多少天即关闭该帐号。 　-
<code>-g</code>修改用户所属的群组。 　-
<code>-G</code>修改用户所属的附加群组。 　-
<code>-l</code>修改用户帐号名称。 　-
<code>-L</code>锁定用户密码，使密码无效。 　-
<code>-s</code>修改用户登入后所使用的shell。 　-
<code>-u</code>修改用户ID。 　- <code>-U</code>解除密码锁定。 　-
实际上最常用的就是<code>-G</code> 　-
不建议修改用户名，否则只修改用户名，初始组...都不变</li>
</ul></li>
<li><p><code>userdel</code>命令</p>
<ul>
<li><code>userdel 用户名</code>可以删除用户</li>
<li><code>-r</code> 同时删除家目录</li>
</ul></li>
<li><p><code>su</code>命令</p>
<ul>
<li><code>su [username]</code>可以切换到其他用户</li>
<li>存在的小问题是<code>su [username]</code>之后home不会切换，方式是加上<code>-</code>选项</li>
<li><code>-c</code>仅执行一次，不切换用户</li>
</ul></li>
</ul>
<h3 id="组管理命令">组管理命令</h3>
<ul>
<li><code>groupadd</code>命令
<ul>
<li>用于添加组</li>
<li><code>-g</code>指定组的GID</li>
</ul></li>
<li><code>groupdel</code>命令
<ul>
<li>删除组</li>
</ul></li>
<li><code>gpasswd</code>命令
<ul>
<li>将用户加入/删除附加组，与<code>usermod</code>一样，但是建议用这个</li>
<li><code>gpasswd -a [username] [groupname]</code>添加组</li>
<li><code>gpasswd -d [username] [groupname]</code>移出组</li>
</ul></li>
<li><code>newgrp</code>命令
<ul>
<li>用于修改有效组(当用户新建文件后文件的所属组是有效组，默认是初始组，可以切换到附加组)</li>
<li>在用户登陆后<code>newgrp 附加组</code></li>
</ul></li>
</ul>
<p>## 权限管理</p>
<p>Linux中权限一共有6种，有wrx,umask两种常见的，还有四种</p>
<h3 id="acl权限">ACL权限</h3>
<p>用于解决用户对文件身份不足的问题，例如一个文件夹，所有者A，所属组B，均有7权限，其他人为0权限，现在要有用户C对文件夹有5权限，那么传统的rwx是无法实现的。在windows中我们可以忽略所属组所有者的概念，直接对用户指定权限，ACL与之类似</p>
<ul>
<li>确定开启ACL：<code>dumpe2fs -h /dev/sad1</code>，ACL权限是和分区绑定的，使用分区查看工具查看分区是否有ACL，看到<code>Default mount options</code>有ACL即可。如果没有挂载可以手动重新挂载<code>mount -o remount,acl /</code>，或者修改fstab</li>
<li><code>setfacl</code>命令
<ul>
<li><code>-m</code>设置ACL
<ul>
<li><code>setfacl -m u:用户名:权限 文件名</code></li>
<li><code>setfacl -m g:组名:权限 文件名</code></li>
<li><code>setfacl -m X:XXX:XXX -R 文件名</code><strong>对已经存在的子文件</strong>递归生效</li>
<li><code>setfacl -m d:X:XXX:XXX -R 文件名</code><strong>对已经存在和以后创建的子文件</strong>递归生效</li>
<li>设置之后尝试ls文件就会发现文件权限变成了<code>-rw-rwxr--+</code>加号表示有ACL权限</li>
</ul></li>
<li><code>-b</code>删除ACL</li>
<li><code>-x 用户</code>删除单个用户ACL</li>
</ul></li>
<li><code>getfacl 文件名</code>查看ACL</li>
</ul>
<p>ACL权限最大的问题是权限溢出，一旦使用了递归赋予ACL，对目录一定要给x权限，那么目录里面的子文件也拥有了x权限，这个是十分危险的，所以还需要手动将子文件的x删除，可以写shell解决，所以能用rwx就用rwx，遇到文件身份不足的时候再用ACL</p>
<h3 id="sudo-授权">sudo 授权</h3>
<p>sudo可以给普通用户赋予部分管理员的权限，权限划分需要配置文件，执行<code>visudo</code></p>
<p>唯一没有注释的一行，意思分别是</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">root    <span class="token assign-left variable">ALL</span><span class="token operator">=</span><span class="token punctuation">(</span>ALL<span class="token punctuation">)</span>       ALL</code></pre>
<ul>
<li>用户名</li>
<li>机器所在的ip，例如，可以写123.123.123.123,111.111.111.111表示只有被控制的机器在这两个ip下这行sudo才有效，但是这个限制方式不是很常用了</li>
<li>可以临时执行的用户，不写就是root</li>
<li>用户可以使用sudo执行哪些命令，必须是命令的绝对路径</li>
</ul>
<p>例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">liukairui    <span class="token number">192.168</span>.1.3,192.168.1.2<span class="token operator">=</span><span class="token punctuation">(</span>ALL<span class="token punctuation">)</span>     /sbin/shutdown </code></pre>
<p>指的是liukairui可以临时执行/sbin/shutdown命令，只有当被控制机器的ip在192.168.1.3,192.168.1.2刚刚这句话才有效</p>
<p>这里的命令必须是绝对路径，用户在执行的时候也必须是绝对路径，如果想写多个命令可以在一个命令后加上<code>,</code>注意，必须是逗号和空格，如果不习惯可以写多行，互不冲突，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">liukairui    <span class="token number">192.168</span>.1.3,192.168.1.2<span class="token operator">=</span><span class="token punctuation">(</span>ALL<span class="token punctuation">)</span>     /sbin/shutdown
liukairui    <span class="token number">192.168</span>.1.3,192.168.1.2<span class="token operator">=</span><span class="token punctuation">(</span>ALL<span class="token punctuation">)</span>     /sbin/reboot</code></pre>
<p>但是一般在分配权限的时候还应该细致考虑，例如我只想让liukairui立刻重启服务器，不想让他关机或者延时重启，应该写</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">liukairui    <span class="token assign-left variable">ALL</span><span class="token operator">=</span><span class="token punctuation">(</span>ALL<span class="token punctuation">)</span>     /sbin/shutdown -r now</code></pre>
<p>命令越细致越安全，也要注意几个例子</p>
<ul>
<li><p>让用户可以修改某个文件</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">liukairui    <span class="token assign-left variable">ALL</span><span class="token operator">=</span><span class="token punctuation">(</span>ALL<span class="token punctuation">)</span>     /usr/bin/vim /path/to/file</code></pre>
<p>而不能只写</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">liukairui    <span class="token assign-left variable">ALL</span><span class="token operator">=</span><span class="token punctuation">(</span>ALL<span class="token punctuation">)</span>     /usr/bin/vim</code></pre>
<p>否则liukairui可以使用vim做所有的事情，这样完全可以修改任意重要的系统文件例如sudoer，相当于给了他服务器所有的控制权</p></li>
<li><p>让用户为其他用户修改密码</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">liukairui    <span class="token assign-left variable">ALL</span><span class="token operator">=</span><span class="token punctuation">(</span>ALL<span class="token punctuation">)</span>     /usr/bin/passwd <span class="token punctuation">[</span>A-Za-z<span class="token punctuation">]</span>*, <span class="token operator">!</span>/usr/bin/passwd <span class="token string">""</span>, <span class="token operator">!</span>/usr/bin/passwd root</code></pre>
<p>这里使用正则与!限制用户不会修改了root密码</p></li>
</ul>
<h3 id="特殊文件权限">特殊文件权限</h3>
<p>特殊文件权限有SetUID，SetGID，StickyBIT，这三个都是Linux为了应对特殊情况设计的，极其危险，极其不推荐自行设置</p>
<p>在普通用户尝试设置密码的时候，密码会存储到<code>/etc/shadow</code>中，但是shadow文件是<code>----------.</code>明显是无法写入的，但是改密码成功了，原因是passwd命令是特殊的，可以查看</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>liukairui@10 ~<span class="token punctuation">]</span>$ ll /usr/bin/passwd
-rwsr-xr-x. <span class="token number">1</span> root root <span class="token number">30768</span> <span class="token number">11</span>月 <span class="token number">24</span> <span class="token number">2015</span> /usr/bin/passwd</code></pre>
<p>passwd的权限是<code>-rwsr-xr-x</code>，<code>rwd</code>变成了<code>rws</code>此时passwd命令具有SetUID，可以修改shadow，所属组是<code>rws</code>表示文件具有SetGID权限，如果其他人权限是<code>rwt</code>表示其他人具有StickyBIT权限</p>
<ul>
<li>SetUID
<ul>
<li>只有二进制文件才可以设置SUID</li>
<li>命令执行者必须具有执行权限(否则权限会显示<code>rwS</code>，S代表报错)</li>
<li>命令的执行者在执行命令文件的时候会临时变为文件的所有者例如passwd会变成root</li>
</ul></li>
<li>SetGID
<ul>
<li>既可以针对执行文件也可以针对目录</li>
<li>对于文件，与SUID类似，只不过是执行的时候所属组身份会变化，代表指令有locate</li>
<li>对于目录，普通用户必须具有7权限才生效，之后用户进入后有效组会变为所属组，例如user进入了某个<code>drwxrwsrwx root:root</code>的文件夹，touch一个文件，文件的所属组会变成root而不是user</li>
</ul></li>
<li>StickyBIT
<ul>
<li>称为粘着位</li>
<li>只对目录有效</li>
<li>普通用户必须拥有wx权限</li>
<li>当设置了粘着位权限后普通用户只能删除这个文件夹里面自己创建的文件，即使有w也不能删除其他人创建的文件，但是root可以无视</li>
</ul></li>
<li>设置方法
<ul>
<li><code>chmod u+s 文件</code>设置SUID</li>
<li><code>chmod g+s 文件</code>设置SGID</li>
<li><code>chmod o+t 文件</code>设置SBIT</li>
<li><code>chmod XYYY 文件</code>，其中YYY是原来是三位数字，例如755，X表示特殊权限
<ul>
<li>4是SUID</li>
<li>2是SGID</li>
<li>1是SBIT</li>
</ul></li>
</ul></li>
</ul>
<h3 id="chattr权限">chattr权限</h3>
<p>这个权限没有危险性，还可以假性的限制root</p>
<ul>
<li><code>chattr</code>命令
<ul>
<li>设置chattr权限</li>
<li>只有root可以运行</li>
<li>使用<code>+-</code>增减权限</li>
<li><code>i</code>权限，文件不可以删除，重命名，修改内容的所有操作</li>
<li><code>a</code>权限，文件只能用<code>echo &gt;&gt;</code>追加内容，对于文件夹就是只能新建和修改?内部的文件</li>
</ul></li>
<li><code>lsattr</code>命令
<ul>
<li>ls不嫩查看chattr权限，只有lsattr可以查看chattr权限</li>
<li><code>-d</code>查询目录的权限</li>
</ul></li>
<li><strong>注意</strong>：有的时候我们无法删除文件，但是lsattr却没有特殊权限，考虑是文件夹设置了chattr权限</li>
</ul>
<h4 id="selinux权限">SELinux权限</h4>
<p>以后再说</p>
<h2 id="文件系统管理">文件系统管理</h2>
<h3 id="硬盘结构与接口">硬盘结构与接口</h3>
<p>略</p>
<h3 id="文件系统">文件系统</h3>
<h4 id="文件系统组成">文件系统组成</h4>
<ul>
<li>super block (超级块)
记录了整个分区的block与inode总量，已经使用与没有使用的block与inode,文件系统的挂载时间与最近校验时间，可以使用<code>dumpe2fs -h /dev/sdax</code>检查</li>
<li>data block (数据块) 用于保存数据的块</li>
<li>inode 用来记录文件的权限，所属组，文件大小...</li>
<li>分区的模型
<ul>
<li>最开始是一个超级块</li>
<li>其余空间由于数量过大，直接做一个inode区不好查，系统还会将剩下的空间分成很多块组(大致200M，使用dumpe2fs
/dev/sda1)</li>
<li>一个块组中包含了
<ul>
<li>inode块</li>
<li>数据块</li>
</ul></li>
</ul></li>
</ul>
<h4 id="常见的文件系统">常见的文件系统</h4>
<p>ext-&gt;ext2-&gt;ext3-&gt;ext4-&gt;xfs</p>
<p>常用的是ext4，文件大小几乎没有限制</p>
<p>从RedHat4(CentOS7)开始就使用了xfs，采用日志形的，丢数据概率低</p>
<h3 id="常用硬盘管理命令">常用硬盘管理命令</h3>
<ul>
<li><code>df</code>命令
<ul>
<li>显示硬盘占用率</li>
<li><code>-h</code>人性化显示</li>
<li><code>-a</code>显示特殊系统文件，例如内存</li>
<li><code>-T</code>显示文件系统类型</li>
</ul></li>
<li><code>du</code>命令
<ul>
<li>ls显示文件夹大小的时候只会显示文件夹文件的大小，也就是文件夹记录的文件名-inode表的大小，想要获取实际的大小<code>du 文件夹</code>查看</li>
<li><code>不加选项</code>显示每个子文件的大小，最后显示文件夹大小</li>
<li><code>-h</code>人性化显示</li>
<li><code>-s</code>只显示总占用</li>
<li><code>-a</code>显示每个子文件夹的大小</li>
</ul></li>
</ul>
<p><strong>df是统计空间大小的，包括的临时文件，没有释放的文件，du是只统计文件大小，两者可能不同，重启即可</strong></p>
<ul>
<li><p><code>fsck</code>命令</p>
<ul>
<li>用于修复文件系统</li>
<li><code>fsck -y /dev/sda1</code></li>
<li>这个命令在意外重启开机后会自动执行，所以如果能修复，开机就修复了，修复不了，手动执行也没有用</li>
</ul></li>
<li><p><code>dumpe2fs</code>命令</p>
<ul>
<li><p>直接执行</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># dumpe2fs /dev/sda5</span>
dumpe2fs <span class="token number">1.41</span>.12 <span class="token punctuation">(</span><span class="token number">17</span>-May-2010<span class="token punctuation">)</span>
Filesystem volume name:   <span class="token operator">&lt;</span>none<span class="token operator">></span>			<span class="token comment"># 卷标</span>
Last mounted on:          /home				<span class="token comment"># 挂载点</span>
Filesystem UUID:          9de2adcf-e7ad-4d49-b032-aadca5924b64	<span class="token comment"># UUID唯一识别符</span>
Filesystem magic number:  0xEF53
Filesystem revision <span class="token comment">#:    1 (dynamic)</span>
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default <span class="token function">mount</span> options:    user_xattr acl	<span class="token comment"># 挂载参数 </span>
Filesystem state:         clean				<span class="token comment"># 文件系统状态，正常</span>
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              <span class="token number">131072</span>			<span class="token comment"># inode总数</span>
Block count:              <span class="token number">524288</span>			<span class="token comment"># 块总数</span>
Reserved block count:     <span class="token number">26214</span>
Free blocks:              <span class="token number">386665</span>
Free inodes:              <span class="token number">66943</span>
First block:              <span class="token number">0</span>
Block size:               <span class="token number">4096</span>				<span class="token comment"># 块大小</span>
Fragment size:            <span class="token number">4096</span>
Reserved GDT blocks:      <span class="token number">127</span>
Blocks per group:         <span class="token number">32768</span>
Fragments per group:      <span class="token number">32768</span>
Inodes per group:         <span class="token number">8192</span>
Inode blocks per group:   <span class="token number">512</span>
Flex block group size:    <span class="token number">16</span>
Filesystem created:       Thu Jan <span class="token number">28</span> <span class="token number">23</span>:28:02 <span class="token number">2021</span>
Last <span class="token function">mount</span> time:          Wed Feb <span class="token number">17</span> <span class="token number">19</span>:39:33 <span class="token number">2021</span>
Last <span class="token function">write</span> time:          Wed Feb <span class="token number">17</span> <span class="token number">19</span>:39:33 <span class="token number">2021</span>
Mount count:              <span class="token number">25</span>
Maximum <span class="token function">mount</span> count:      -1
Last checked:             Thu Jan <span class="token number">28</span> <span class="token number">23</span>:28:02 <span class="token number">2021</span>
Check interval:           <span class="token number">0</span> <span class="token punctuation">(</span><span class="token operator">&lt;</span>none<span class="token operator">></span><span class="token punctuation">)</span>
Lifetime writes:          <span class="token number">962</span> MB
Reserved blocks uid:      <span class="token number">0</span> <span class="token punctuation">(</span>user root<span class="token punctuation">)</span>
Reserved blocks gid:      <span class="token number">0</span> <span class="token punctuation">(</span>group root<span class="token punctuation">)</span>
First inode:              <span class="token number">11</span>
Inode size:               <span class="token number">256</span>				<span class="token comment"># inode大小</span>
Required extra isize:     <span class="token number">28</span>
Desired extra isize:      <span class="token number">28</span>
Journal inode:            <span class="token number">8</span>
Default directory hash:   half_md4
Directory Hash Seed:      f23ce554-4779-4435-93e1-6048b842d72b
Journal backup:           inode blocks
Journal features:         journal_incompat_revoke
日志大小:             64M
Journal length:           <span class="token number">16384</span>
Journal sequence:         0x000000f9
Journal start:            <span class="token number">1</span>

Group <span class="token number">0</span>: <span class="token punctuation">(</span>Blocks <span class="token number">0</span>-32767<span class="token punctuation">)</span> <span class="token punctuation">[</span>ITABLE_ZEROED<span class="token punctuation">]</span>
  校验和 0x2b5a,0个未使用的inode
  主 superblock at <span class="token number">0</span>, Group descriptors at <span class="token number">1</span>-1
  保留的GDT块位于 <span class="token number">2</span>-128
  Block bitmap at <span class="token number">129</span> <span class="token punctuation">(</span>+129<span class="token punctuation">)</span>, Inode bitmap at <span class="token number">145</span> <span class="token punctuation">(</span>+145<span class="token punctuation">)</span>
  Inode表位于 <span class="token number">161</span>-672 <span class="token punctuation">(</span>+161<span class="token punctuation">)</span>
  <span class="token number">22319</span> <span class="token function">free</span> blocks, <span class="token number">1</span> <span class="token function">free</span> inodes, <span class="token number">289</span> directories
  可用块数: <span class="token number">10449</span>-32767
  可用inode数: <span class="token number">18</span>
<span class="token punctuation">..</span>.块信息<span class="token punctuation">..</span>.</code></pre></li>
<li><p><code>-h</code>查看超级块信息(没有后面的Group 0)</p></li>
</ul></li>
<li><p><code>stat</code>命令</p>
<ul>
<li><p>查看文件的详细信息</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># stat webmin-1.970</span>
  File: <span class="token string">"webmin-1.970"</span>
  Size: <span class="token number">12288</span>           Blocks: <span class="token number">24</span>         IO Block: <span class="token number">4096</span>   目录
  <span class="token comment">#     文件大小         占用块              分区大小</span>
Device: 805h/2053d      Inode: <span class="token number">3621</span>        Links: <span class="token number">132</span>
  <span class="token comment">#     设备inode        inode              硬链接数</span>
Access: <span class="token punctuation">(</span>0755/drwxr-xr-x<span class="token punctuation">)</span>  Uid: <span class="token punctuation">(</span>    <span class="token number">0</span>/    root<span class="token punctuation">)</span>   Gid: <span class="token punctuation">(</span>    <span class="token number">1</span>/     bin<span class="token punctuation">)</span>
  <span class="token comment">#      权限              所有者                    所属组</span>
Access: <span class="token number">2021</span>-02-16 <span class="token number">22</span>:44:31.673999983 +0800
<span class="token comment"># 访问时间</span>
Modify: <span class="token number">2021</span>-01-06 07:42:06.000000000 +0800
<span class="token comment"># 修改时间</span>
Change: <span class="token number">2021</span>-02-03 <span class="token number">23</span>:41:33.988999887 +0800
<span class="token comment"># 状态修改时间</span></code></pre></li>
</ul></li>
<li><p><code>type</code>命令</p>
<ul>
<li><p>判断命令的类型</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># type cd</span>
<span class="token builtin class-name">cd</span> is a shell <span class="token builtin class-name">builtin</span>				<span class="token comment"># 内建命令</span>
<span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># type mkdir</span>
<span class="token function">mkdir</span> is hashed <span class="token punctuation">(</span>/bin/mkdir<span class="token punctuation">)</span>
<span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># type /bin/mkdir</span>
/bin/mkdir is /bin/mkdir			<span class="token comment"># 是个命令</span>
<span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># type ls</span>
<span class="token function">ls</span> is aliased to `ls --color<span class="token operator">=</span>auto'	 <span class="token comment"># 是个别名</span></code></pre></li>
</ul></li>
<li><p><code>file</code>命令</p>
<ul>
<li><p>用于判断文件类型</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># file httpd-2.2.9.tar.bz2 </span>
httpd-2.2.9.tar.bz2: <span class="token function">bzip2</span> compressed data, block size <span class="token operator">=</span> 900k
<span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># file demo </span>
demo: ASCII text
<span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># file /sbin/shutdown </span>
/sbin/shutdown: ELF <span class="token number">64</span>-bit LSB shared object, x86-64, version <span class="token number">1</span> <span class="token punctuation">(</span>SYSV<span class="token punctuation">)</span>, dynamically linked <span class="token punctuation">(</span>uses shared libs<span class="token punctuation">)</span>, <span class="token keyword">for</span> GNU/Linux <span class="token number">2.6</span>.18, stripped</code></pre></li>
</ul></li>
</ul>
<h3 id="手工分区的方法">手工分区的方法</h3>
<ul>
<li><p><code>fdisk -l</code><strong>查询</strong>分区状态</p></li>
<li><p><code>fdisk /dev/sdb</code>对sdb进行交互式<strong>分区</strong></p>
<ul>
<li><p><code>m</code>获取帮助</p></li>
<li><p><code>n</code>新建</p></li>
<li><p><code>d</code>删除</p></li>
<li><p><code>p</code>打印分区</p></li>
<li><p><code>l</code>列数可识别分区</p></li>
<li><p><code>t</code>改变系统id</p></li>
<li><p><code>q</code>不保存退出</p></li>
<li><p><code>w</code>保存退出</p></li>
<li><p>新建一个分区</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: n
Command action
   e   extended
   p   primary partition <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>
p									<span class="token comment"># 询问新建一个什么分区，第一个分区当然是主分区</span>
Partition number <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>: <span class="token number">1</span>			  <span class="token comment"># 主分区号，按顺序写，否则以前的难以利用</span>
First cylinder <span class="token punctuation">(</span><span class="token number">1</span>-2610, default <span class="token number">1</span><span class="token punctuation">)</span>:    <span class="token comment"># 起始柱面，直接回车默认</span>
Using default value <span class="token number">1</span>
Last cylinder, +cylinders or +size<span class="token punctuation">&#123;</span>K,M,G<span class="token punctuation">&#125;</span> <span class="token punctuation">(</span><span class="token number">1</span>-2610, default <span class="token number">2610</span><span class="token punctuation">)</span>: +2G
<span class="token comment"># 终止柱面，因为难以计算，可以直接写大小，fdisk自己计算，注意的是因为最小单位是柱面，所以如果容量不能正好分完一个柱面则会选择一个就近的值，所以结果会比指定的大一点或者小一点</span></code></pre></li>
<li><p>新建一个逻辑分区</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: n
Command action
   e   extended
   p   primary partition <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>
e								<span class="token comment"># 新建一个扩展分区</span>
Partition number <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>: <span class="token number">2</span>		  <span class="token comment"># 选择编号2</span>
First cylinder <span class="token punctuation">(</span><span class="token number">263</span>-2610, default <span class="token number">263</span><span class="token punctuation">)</span>: 	<span class="token comment"># 将逻辑分区占满空闲区域</span>
Using default value <span class="token number">263</span>
Last cylinder, +cylinders or +size<span class="token punctuation">&#123;</span>K,M,G<span class="token punctuation">&#125;</span> <span class="token punctuation">(</span><span class="token number">263</span>-2610, default <span class="token number">2610</span><span class="token punctuation">)</span>: 
Using default value <span class="token number">2610</span>
Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: n       		<span class="token comment"># 新建逻辑分区</span>
Command action
   l   logical <span class="token punctuation">(</span><span class="token number">5</span> or over<span class="token punctuation">)</span>			<span class="token comment"># 会自动换成逻辑，同时编号自动变成5+</span>
   p   primary partition <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>
l
First cylinder <span class="token punctuation">(</span><span class="token number">263</span>-2610, default <span class="token number">263</span><span class="token punctuation">)</span>: 	<span class="token comment"># 起始扇区默认</span>
Using default value <span class="token number">263</span>
Last cylinder, +cylinders or +size<span class="token punctuation">&#123;</span>K,M,G<span class="token punctuation">&#125;</span> <span class="token punctuation">(</span><span class="token number">263</span>-2610, default <span class="token number">2610</span><span class="token punctuation">)</span>: +2G		<span class="token comment"># 给2G</span></code></pre></li>
<li><p>w推出后可能会报硬盘忙error16，需要重启才可以格式化</p></li>
</ul></li>
<li><p><code>mkfs -t ext4 /dev/sdb1</code>对分区<strong>格式化</strong></p>
<p>如果想要设置高级的格式化参数可以使用<code>mke2fs</code>，但是强烈不推荐</p></li>
<li><p><code>mount</code><strong>挂载</strong></p></li>
<li><p>设置<strong>自动挂载</strong></p>
<p>编辑/etc/fstab</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable">UUID</span><span class="token operator">=</span>7413895f-b2f5-4d15-80eb-422af604c847 /                       ext4    defaults        <span class="token number">1</span> <span class="token number">1</span>
<span class="token assign-left variable">UUID</span><span class="token operator">=</span>95fb2e8e-3bac-4270-83ff-938368cbfa34 /boot                   ext4    defaults        <span class="token number">1</span> <span class="token number">2</span>
<span class="token assign-left variable">UUID</span><span class="token operator">=</span>9de2adcf-e7ad-4d49-b032-aadca5924b64 /home                   ext4    defaults        <span class="token number">1</span> <span class="token number">2</span>
<span class="token assign-left variable">UUID</span><span class="token operator">=</span>86b5cdd1-de85-431c-a144-2d0eea95ebb6 swap                    swap    defaults        <span class="token number">0</span> <span class="token number">0</span>
tmpfs                   /dev/shm                tmpfs   defaults        <span class="token number">0</span> <span class="token number">0</span>
devpts                  /dev/pts                devpts  <span class="token assign-left variable">gid</span><span class="token operator">=</span><span class="token number">5</span>,mode<span class="token operator">=</span><span class="token number">620</span>  <span class="token number">0</span> <span class="token number">0</span>
sysfs                   /sys                    sysfs   defaults        <span class="token number">0</span> <span class="token number">0</span>
proc                    /proc                   proc    defaults        <span class="token number">0</span> <span class="token number">0</span></code></pre>
<ul>
<li><p>第一列分区的UUID,这里建议写UUID而不是/dev/sdb1的原因是方式更换硬盘后系统将新盘的第一个分区误认为是sdb1，可以这样查看UUID</p>
<p><pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># ll /dev/disk/by-uuid/</span>
总用量 <span class="token number">0</span>
lrwxrwxrwx. <span class="token number">1</span> root root <span class="token number">10</span> <span class="token number">2</span>月  <span class="token number">17</span> <span class="token number">23</span>:07 1c974240-05fc-45da-a574-34d62ada86b2 -<span class="token operator">></span> <span class="token punctuation">..</span>/<span class="token punctuation">..</span>/sdb5
lrwxrwxrwx. <span class="token number">1</span> root root <span class="token number">10</span> <span class="token number">2</span>月  <span class="token number">17</span> <span class="token number">23</span>:07 45e3bdda-5297-44bd-950f-c7da1eefaec2 -<span class="token operator">></span> <span class="token punctuation">..</span>/<span class="token punctuation">..</span>/sdb1
lrwxrwxrwx. <span class="token number">1</span> root root <span class="token number">10</span> <span class="token number">2</span>月  <span class="token number">17</span> <span class="token number">22</span>:09 7413895f-b2f5-4d15-80eb-422af604c847 -<span class="token operator">></span> <span class="token punctuation">..</span>/<span class="token punctuation">..</span>/sda2
lrwxrwxrwx. <span class="token number">1</span> root root <span class="token number">10</span> <span class="token number">2</span>月  <span class="token number">17</span> <span class="token number">22</span>:09 86b5cdd1-de85-431c-a144-2d0eea95ebb6 -<span class="token operator">></span> <span class="token punctuation">..</span>/<span class="token punctuation">..</span>/sda3
lrwxrwxrwx. <span class="token number">1</span> root root <span class="token number">10</span> <span class="token number">2</span>月  <span class="token number">17</span> <span class="token number">22</span>:09 95fb2e8e-3bac-4270-83ff-938368cbfa34 -<span class="token operator">></span> <span class="token punctuation">..</span>/<span class="token punctuation">..</span>/sda1
lrwxrwxrwx. <span class="token number">1</span> root root <span class="token number">10</span> <span class="token number">2</span>月  <span class="token number">17</span> <span class="token number">22</span>:09 9de2adcf-e7ad-4d49-b032-aadca5924b64 -<span class="token operator">></span> <span class="token punctuation">..</span>/<span class="token punctuation">..</span>/sda5</code></pre></p></li>
<li><p>第二列是挂载点</p></li>
<li><p>第三列是文件系统</p></li>
<li><p>第四列是挂载选项</p></li>
<li><p>第五列是文件是否可以被备份</p>
<ul>
<li>0 不备份</li>
<li>1 每天备份(推荐选择)</li>
<li>2 不定期备份</li>
</ul></li>
<li><p>第六列是是否自动使用fsck自动检测</p>
<ul>
<li>0 不检测</li>
<li>1 启动时检测(/需要)</li>
<li>2 启动后检测(除了/之外的选择这个)</li>
</ul></li>
</ul></li>
<li><p><code>/etc/fstab</code><strong>文件修复</strong></p>
<ul>
<li>一切修复的前提是拿到本机...</li>
<li>按提示登录</li>
<li>重新挂载根分区<code>mount -o remount,wr /</code>来修改文件</li>
</ul></li>
<li><p><code>parted</code>命令</p>
<p>目前有GPT和MBR，其中fdisk只能做MBR分区，可以使用parted进行GPT分区</p>
<ul>
<li><p><code>print</code>打印分区，<code>Partition Table: msdos</code>说明是MBR</p></li>
<li><p><code>mklabel gpt</code>修改分区表</p></li>
<li><p>建立新分区</p>
<pre class="language-none"><code class="language-none">(parted) mkpart
分区名称？  []? disk1                                      # 设置一个免得后面麻烦               
文件系统类型？  [ext2]?                                     # 回车               
起始点？ 1M                                                # 不能回车，直接写1M                
结束点？ 2G                                                # 写大小 
(parted) mkfs                                             # 分区          
分区编号？ 1                                                              
文件系统类型？  [ext2]?                                     # 回车              
(parted) q                                                                
信息: You may need to update &#x2F;etc&#x2F;fstab.                                  
[root@10 liukairui]# mkfs -t ext4 &#x2F;dev&#x2F;sdb1				# 格式化为ext4</code></pre></li>
<li><p>改分区大小</p>
<p>有很大风险，不建议</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 liukairui<span class="token punctuation">]</span><span class="token comment"># parted /dev/sdb</span>
<span class="token punctuation">(</span>parted<span class="token punctuation">)</span> resize
分区编号？ <span class="token number">1</span>                                                              
起始点？  <span class="token punctuation">[</span>1049kB<span class="token punctuation">]</span>?                                                       
结束点？  <span class="token punctuation">[</span>2000MB<span class="token punctuation">]</span>? 3G              </code></pre></li>
</ul></li>
<li><p>建立/增加<strong>swap分区</strong></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># fdisk /dev/sdb</span>
Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: n
Command action
   e   extended
   p   primary partition <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>
p
Partition number <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>: <span class="token number">1</span>
First cylinder <span class="token punctuation">(</span><span class="token number">1</span>-2610, default <span class="token number">1</span><span class="token punctuation">)</span>: 
Using default value <span class="token number">1</span>
Last cylinder, +cylinders or +size<span class="token punctuation">&#123;</span>K,M,G<span class="token punctuation">&#125;</span> <span class="token punctuation">(</span><span class="token number">1</span>-2610, default <span class="token number">2610</span><span class="token punctuation">)</span>: +8G

Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: t
Selected partition <span class="token number">1</span>
Hex code <span class="token punctuation">(</span>type L to list codes<span class="token punctuation">)</span>: <span class="token number">82</span>			<span class="token comment"># 修改文件类型，这个可以在l查到</span>
Changed system <span class="token builtin class-name">type</span> of partition <span class="token number">1</span> to <span class="token number">82</span> <span class="token punctuation">(</span>Linux swap / Solaris<span class="token punctuation">)</span>
Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: w
The partition table has been altered<span class="token operator">!</span>

<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># mkswap /dev/sdb1				# 格式化</span>
Setting up swapspace version <span class="token number">1</span>, size <span class="token operator">=</span> <span class="token number">8393924</span> KiB
no label, <span class="token assign-left variable">UUID</span><span class="token operator">=</span>a5b2a742-ee50-42f3-aaff-9e4258df52c5
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># swapon /dev/sdb1				# 挂载</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># free -h</span>
             total       used       <span class="token function">free</span>     shared    buffers     cached
Mem:          996M       279M       717M       184K        24M       112M
-/+ buffers/cache:       142M       853M
Swap:          10G         0B        10G	<span class="token comment"># 已经多了8G</span></code></pre>
<p>永久挂载只需要修改<code>/etc/fstab</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable">UUID</span><span class="token operator">=</span>86b5cdd1-de85-431c-a144-2d0eea95ebb6 swap                    swap    defaults        <span class="token number">0</span> <span class="token number">0</span></code></pre></li>
</ul>
<h2 id="高级文件系统管理">高级文件系统管理</h2>
<h3 id="磁盘配额">磁盘配额</h3>
<p>限制用户存储文件的数目与大小</p>
<ul>
<li><p>磁盘配额的条件</p>
<ul>
<li><p>内核支持磁盘配额</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># grep CONFIG_QUOTA /boot/config-2.6.32-642.el6.x86_64 </span>
<span class="token assign-left variable">CONFIG_QUOTA</span><span class="token operator">=</span>y							<span class="token comment"># 找到了这些项就正常</span>
<span class="token assign-left variable">CONFIG_QUOTA_NETLINK_INTERFACE</span><span class="token operator">=</span>y
<span class="token comment"># CONFIG_QUOTA_DEBUG is not set</span>
<span class="token assign-left variable">CONFIG_QUOTA_TREE</span><span class="token operator">=</span>y
<span class="token assign-left variable">CONFIG_QUOTACTL</span><span class="token operator">=</span>y</code></pre></li>
<li><p>检查系统中安装了quota工具</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># rpm -qa | grep quota</span>
quota-3.17-23.el6.x86_64</code></pre></li>
</ul></li>
<li><p>概念</p>
<ul>
<li><p>用户配额和组配额</p>
<p>组配额用的少，因为组内的配额不是平均分配的，可能一个用户直接写满了配额其他人不能写了</p></li>
<li><p>磁盘容量与文件个数限制</p>
<p>限制文件个数主要是为了防止inode写满</p></li>
<li><p>软限制和硬限制</p>
<p>用户在写入超过软限制后还可以继续写入，但是会被警告，超过硬限制则无法写入</p></li>
<li><p>宽限时间</p>
<p>如果用户处于软硬限制之间，系统会警告用户，如果超过宽限时间用户还是在超过软限制，软限制会升级为硬限制</p></li>
</ul></li>
<li><p>磁盘配额是<strong>普通用户</strong>在使用<strong>分区</strong>的限制，超级用户可以无视</p></li>
<li><p>在分区中开启配额功能</p>
<p>临时开启</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># mount -o remount,usrquota,grpquota /disk</span></code></pre>
<p>永久开启</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable">UUID</span><span class="token operator">=</span>93aa9645-bdde-4a95-b9cb-29e536711869 /disk ext4 defaults,usrquota,grpquota <span class="token number">1</span> <span class="token number">2</span></code></pre></li>
<li><p>在分区中建立磁盘配额文件</p>
<ul>
<li><p>使用<code>quotacheck</code>命令</p>
<ul>
<li><code>-a</code> 扫描所有开启磁盘配额的分区</li>
<li><code>-c</code> 不管是否有配置文件，出现扫描并建立新配置文件</li>
<li><code>-u</code> 建立用户配额文件</li>
<li><code>-g</code> 建立组配额文件</li>
<li><code>-v</code> 显示扫描过程</li>
<li><code>-m</code> 强制读写方式扫描</li>
<li><code>-f</code> 强制扫描文件系统</li>
<li>常用命令<code>quotacheck -avug</code></li>
<li>特别的，如果要给<code>/</code>开启配额，需要写<code>quotacheck -avugm</code></li>
</ul>
<p>使用这个命令必须关闭SELinux</p></li>
<li><p>关闭SELinux</p></li>
</ul>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># getenforce 		# 查看当前状态</span>
Enforcing
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># setenforce 0		# 临时关闭</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># getenforce 		# 查看状态</span>
Permissive
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># vim /etc/selinux/config 	# 永久修改(要重启)</span>
<span class="token assign-left variable">SELINUX</span><span class="token operator">=</span>enforcing -<span class="token operator">></span> disabled</code></pre>
<ul>
<li><p>检查开启了磁盘配额</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># ll /disk/</span>
总用量 <span class="token number">32</span>
-rw-------. <span class="token number">1</span> root root  <span class="token number">6144</span> <span class="token number">2</span>月  <span class="token number">18</span> <span class="token number">11</span>:17 aquota.group
-rw-------. <span class="token number">1</span> root root  <span class="token number">6144</span> <span class="token number">2</span>月  <span class="token number">18</span> <span class="token number">11</span>:17 aquota.user
drwx------. <span class="token number">2</span> root root <span class="token number">16384</span> <span class="token number">2</span>月  <span class="token number">18</span> <span class="token number">10</span>:25 lost+found</code></pre>
<p>看到了aquota.user/group</p></li>
</ul></li>
<li><p>设置用户和组的配额</p>
<p><code>edquota</code>命令</p>
<ul>
<li><code>-u</code> 设置用户配额</li>
<li><code>-g</code> 设置组配额</li>
<li><code>-t</code> 设置宽限时间</li>
<li><code>-p</code> 复制配额方案</li>
</ul>
<p>例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># edquota -u user1</span>
Disk quotas <span class="token keyword">for</span> user user1 <span class="token punctuation">(</span>uid <span class="token number">501</span><span class="token punctuation">)</span>:		<span class="token comment"># 为user1 UID=501 配置</span>
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdb1                         <span class="token number">0</span>          <span class="token number">0</span>          <span class="token number">0</span>          <span class="token number">0</span>        <span class="token number">0</span>        <span class="token number">0</span>
<span class="token comment"># 分区			    用户已经占用block(KB) block软限制  硬限制   已用inode  软限制    硬限制</span>
<span class="token punctuation">[</span>root@10 disk<span class="token punctuation">]</span><span class="token comment"># edquota -p user1 -u user2	# 复制配额</span>
<span class="token punctuation">[</span>root@10 disk<span class="token punctuation">]</span><span class="token comment"># quota -uvs user2</span>
Disk quotas <span class="token keyword">for</span> user user2 <span class="token punctuation">(</span>uid <span class="token number">502</span><span class="token punctuation">)</span>: 
     Filesystem  blocks   <span class="token function">quota</span>   limit   grace   files   <span class="token function">quota</span>   limit   grace
      /dev/sdb1       <span class="token number">0</span>   <span class="token number">40000</span>   <span class="token number">50000</span>               <span class="token number">0</span>       <span class="token number">8</span>      <span class="token number">11</span>        
<span class="token punctuation">[</span>root@10 disk<span class="token punctuation">]</span><span class="token comment"># edquota -t					# 修改宽限时间</span>
Grace period before enforcing soft limits <span class="token keyword">for</span> users:
Time <span class="token function">units</span> may be: days, hours, minutes, or seconds
  Filesystem             Block grace period     Inode grace period
  /dev/sdb1                     8days                  7days</code></pre></li>
<li><p>启动和关闭配额</p>
<ul>
<li><code>quotaon</code>启动配额
<ul>
<li><code>-a</code> 启动所有配额分区</li>
<li><code>-u</code> 启动用户配额</li>
<li><code>-g</code> 启动组配额</li>
<li><code>-v</code> 显示过程</li>
</ul></li>
<li><code>quotaoff</code>关闭配额
<ul>
<li><code>-a</code> 启动所有配额分区</li>
<li><code>-u</code> 启动用户配额</li>
<li><code>-g</code> 启动组配额</li>
</ul></li>
</ul></li>
<li><p>查询磁盘配额</p>
<ul>
<li><code>quota -uvs 用户名</code>查询用户配额(<code>-v</code>:详细信息，<code>-s</code>：人性化显示)</li>
<li><code>quota -gvs 组名</code>查询组配额</li>
<li><code>repquota -avus</code>查询分区配额</li>
</ul></li>
<li><p>测试配额</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">dd</span> <span class="token assign-left variable">if</span><span class="token operator">=</span>/dev/zero <span class="token assign-left variable">of</span><span class="token operator">=</span>test <span class="token assign-left variable">bs</span><span class="token operator">=</span>1M <span class="token assign-left variable">count</span><span class="token operator">=</span><span class="token number">60</span>
sdb1: warning, user block <span class="token function">quota</span> exceeded.
sdb1: <span class="token function">write</span> failed, user block limit reached.
dd: 正在写入<span class="token string">"test"</span><span class="token builtin class-name">:</span> 超出磁盘限额
记录了49+0 的读入
记录了48+0 的写出
<span class="token number">51195904</span>字节<span class="token punctuation">(</span><span class="token number">51</span> MB<span class="token punctuation">)</span>已复制，0.0318794 秒，1.6 GB/秒
<span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">touch</span> <span class="token number">1</span>
<span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">touch</span> <span class="token number">2</span>
<span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">touch</span> <span class="token number">3</span>
<span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">touch</span> <span class="token number">4</span>
<span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">touch</span> <span class="token number">5</span>
<span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">touch</span> <span class="token number">6</span>
<span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">touch</span> <span class="token number">7</span>
<span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">touch</span> <span class="token number">8</span>
<span class="token punctuation">[</span>user1@10 disk<span class="token punctuation">]</span>$ <span class="token function">touch</span> <span class="token number">9</span>
sdb1: <span class="token function">write</span> failed, user <span class="token function">file</span> limit reached.
touch: 无法创建<span class="token string">"9"</span><span class="token builtin class-name">:</span> 超出磁盘限额</code></pre>
<p>dd解释：拷贝文件，来自<code>/dev/zero</code>到test,拷贝大小1M,拷贝60次</p></li>
<li><p><code>setqupta</code>命令可以以非交互的形式配置配额</p></li>
</ul>
<h3 id="lvm逻辑卷管理">LVM逻辑卷管理</h3>
<h4 id="基本原理">基本原理</h4>
<p>LVM是Logical Volume
Manager的简称，就是逻辑卷管理，最大的特征是可以动态调整分区大小</p>
<p>LVM的概念</p>
<ul>
<li>物理卷PV：真正的无力硬盘或者分区</li>
<li>卷组VG：由多个PV合成的一个逻辑上的硬盘</li>
<li>逻辑卷LV：是将VG分区得到的，可以想象成将这个逻辑上的硬盘进行了分区</li>
<li>物理扩展PE：保存数据的最小单元，默认4M</li>
</ul>
<p>LVM原理</p>
<ul>
<li>将普通的分区变成PV</li>
<li>一个或者多个PV组成一个VG，相当于重组了一个新硬盘，VG可以随时增加PV扩展大小</li>
<li>在VG上划分新的LV,相当于在新硬盘上分区，此时的LV可以从VG上扩展分区</li>
</ul>
<p>LVM，MBR，GPT，ext....的关系</p>
<ul>
<li><p>MBR，GPT是硬盘最基本的内容，是分区表的格式</p></li>
<li><p>在MBR...的基础上我们会进行分区，这个时候就会有诸如标准分区(sda1,sda2)或者swap分区，或者LVM分区的概念，几者同级(在fdisk
的l可以看到)，不论这个盘是GPT还是MBR都可以创建普通分区或者LVM分区，只不过是分区为MBR分区还是GPT分区</p></li>
<li><p>得到分区之后我们需要对分区写入文件系统，这个时候就会有ext4,vfat,iso9660,xfs......，对于标准分区我们会直接格式化对于LVM分区我们也要在LV上进行格式化</p></li>
<li><p>一张图解释</p>
<pre class="mermaid">  graph LR
HD(硬盘)
MBRGPT(MBR/GPT)
part(一个普通的分区)
stand(标准分区)
swap(swap分区)
PVP1(LVM分区1)
PVP2(LVM分区2)
PVP3(LVM分区3)
stus(可用的ext4标准分区)
lv1us(可用的ext4LVM分区1)
lv2us(可用的ext4LVM分区2)
spus(可用的swap)
  subgraph LVM
  PV1(PV1)
  PV2(PV2)
  PV3(PV3)
  VG(VG)
  LV1(LV1)
  LV2(LV2)
  LVMORE(LV...)
  end
HD --指定 --> MBRGPT
MBRGPT --划分出--> part
part --默认是--> stand
part --fdisk t --> swap
part --fdisk t --> PVP1 --创建PV--> PV1
part --fdisk t --> PVP2 --创建PV--> PV2
part --fdisk t --> PVP3 --创建PV--> PV3
PV1 ---> VG
PV2 ---> VG
PV3 ---> VG
VG --分区--> LV1
VG --分区--> LV2
VG --分区--> LVMORE
stand --mkfs ext4写入文件系统--> stus
LV1 --mkfs ext4写入文件系统--> lv1us
LV2 --mkfs ext4写入文件系统--> lv2us
swap --mkswap写入文件系统--> spus
HD -.逻辑同级.- VG
swap -.逻辑同级.- LV1 -.逻辑同级.- LV2 -.逻辑同级.- LVMORE
stand -.同级.- swap -.同级.- PVP1 -.同级.- PVP2 -.同级.- PVP3
stus -.同级.- spus -.同级.- lv1us -.同级.- lv2us</pre></li>
</ul>
<h4 id="图形界面下分区">图形界面下分区</h4>
<p>可以在CentOS 的安装界面下进行LVM分区，进入到手动分区界面</p>
<ul>
<li>在sda下创建200M的标准分区给/boot(/boot不能用LVM)</li>
<li>选空闲，创建，LVM物理卷，这里相当于得到了一个PV，准备一个PV组一个VG，使用全部可用空间，至此得到了一个PV叫做sda2</li>
<li>选sda2，创建，LVM卷组，得到VG，此时这个VG是和sda同级的，相当于一个硬盘</li>
<li>选空闲，创建，LVM逻辑卷，选择VG，创建，选择文件系统和挂载点即可得到<code>/</code>,<code>/home/</code>,<code>swap</code></li>
</ul>
<p>进入系统，我们可以查看</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># fdisk -l</span>

Disk /dev/sda: <span class="token number">21.5</span> GB, <span class="token number">21474836480</span> bytes
<span class="token number">255</span> heads, <span class="token number">63</span> sectors/track, <span class="token number">2610</span> cylinders
Units <span class="token operator">=</span> cylinders of <span class="token number">16065</span> * <span class="token number">512</span> <span class="token operator">=</span> <span class="token number">8225280</span> bytes
Sector size <span class="token punctuation">(</span>logical/physical<span class="token punctuation">)</span>: <span class="token number">512</span> bytes / <span class="token number">512</span> bytes
I/O size <span class="token punctuation">(</span>minimum/optimal<span class="token punctuation">)</span>: <span class="token number">512</span> bytes / <span class="token number">512</span> bytes
Disk identifier: 0x000798de

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           <span class="token number">1</span>          <span class="token number">26</span>      <span class="token number">204800</span>   <span class="token number">83</span>  Linux
Partition <span class="token number">1</span> does not end on cylinder boundary.
/dev/sda2              <span class="token number">26</span>        <span class="token number">2611</span>    <span class="token number">20765696</span>   8e  Linux LVM

Disk /dev/mapper/VolGroup-LogVol01: <span class="token number">2147</span> MB, <span class="token number">2147483648</span> bytes
<span class="token number">255</span> heads, <span class="token number">63</span> sectors/track, <span class="token number">261</span> cylinders
Units <span class="token operator">=</span> cylinders of <span class="token number">16065</span> * <span class="token number">512</span> <span class="token operator">=</span> <span class="token number">8225280</span> bytes
Sector size <span class="token punctuation">(</span>logical/physical<span class="token punctuation">)</span>: <span class="token number">512</span> bytes / <span class="token number">512</span> bytes
I/O size <span class="token punctuation">(</span>minimum/optimal<span class="token punctuation">)</span>: <span class="token number">512</span> bytes / <span class="token number">512</span> bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-LogVol00: <span class="token number">10.7</span> GB, <span class="token number">10737418240</span> bytes
<span class="token number">255</span> heads, <span class="token number">63</span> sectors/track, <span class="token number">1305</span> cylinders
Units <span class="token operator">=</span> cylinders of <span class="token number">16065</span> * <span class="token number">512</span> <span class="token operator">=</span> <span class="token number">8225280</span> bytes
Sector size <span class="token punctuation">(</span>logical/physical<span class="token punctuation">)</span>: <span class="token number">512</span> bytes / <span class="token number">512</span> bytes
I/O size <span class="token punctuation">(</span>minimum/optimal<span class="token punctuation">)</span>: <span class="token number">512</span> bytes / <span class="token number">512</span> bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-LogVol02: <span class="token number">8376</span> MB, <span class="token number">8376025088</span> bytes
<span class="token number">255</span> heads, <span class="token number">63</span> sectors/track, <span class="token number">1018</span> cylinders
Units <span class="token operator">=</span> cylinders of <span class="token number">16065</span> * <span class="token number">512</span> <span class="token operator">=</span> <span class="token number">8225280</span> bytes
Sector size <span class="token punctuation">(</span>logical/physical<span class="token punctuation">)</span>: <span class="token number">512</span> bytes / <span class="token number">512</span> bytes
I/O size <span class="token punctuation">(</span>minimum/optimal<span class="token punctuation">)</span>: <span class="token number">512</span> bytes / <span class="token number">512</span> bytes
Disk identifier: 0x00000000

<span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># mount</span>
/dev/mapper/VolGroup-LogVol00 on / <span class="token builtin class-name">type</span> ext4 <span class="token punctuation">(</span>rw<span class="token punctuation">)</span>
proc on /proc <span class="token builtin class-name">type</span> proc <span class="token punctuation">(</span>rw<span class="token punctuation">)</span>
sysfs on /sys <span class="token builtin class-name">type</span> sysfs <span class="token punctuation">(</span>rw<span class="token punctuation">)</span>
devpts on /dev/pts <span class="token builtin class-name">type</span> devpts <span class="token punctuation">(</span>rw,gid<span class="token operator">=</span><span class="token number">5</span>,mode<span class="token operator">=</span><span class="token number">620</span><span class="token punctuation">)</span>
tmpfs on /dev/shm <span class="token builtin class-name">type</span> tmpfs <span class="token punctuation">(</span>rw,rootcontext<span class="token operator">=</span><span class="token string">"system_u:object_r:tmpfs_t:s0"</span><span class="token punctuation">)</span>
/dev/sda1 on /boot <span class="token builtin class-name">type</span> ext4 <span class="token punctuation">(</span>rw<span class="token punctuation">)</span>
/dev/mapper/VolGroup-LogVol02 on /home <span class="token builtin class-name">type</span> ext4 <span class="token punctuation">(</span>rw<span class="token punctuation">)</span>
none on /proc/sys/fs/binfmt_misc <span class="token builtin class-name">type</span> binfmt_misc <span class="token punctuation">(</span>rw<span class="token punctuation">)</span></code></pre>
<p>fdisk会方便列出LVM分区的详细信息，mount会直接说</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">/dev/mapper/VolGroup-LogVol00 on / <span class="token builtin class-name">type</span> ext4 <span class="token punctuation">(</span>rw<span class="token punctuation">)</span>
/dev/sda1 on /boot <span class="token builtin class-name">type</span> ext4 <span class="token punctuation">(</span>rw<span class="token punctuation">)</span>
/dev/mapper/VolGroup-LogVol02 on /home <span class="token builtin class-name">type</span> ext4 <span class="token punctuation">(</span>rw<span class="token punctuation">)</span></code></pre>
<p>/boot挂载在了sda1上，其他两个直接挂载到了另一个硬件上，这个新硬盘就是我们的VG，当时没有重命名于是给的是默认命名，后面加上1-3就三个LV，可以
查证</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># ll /dev/mapper/</span>
总用量 <span class="token number">0</span>
crw-rw----. <span class="token number">1</span> root root <span class="token number">10</span>, <span class="token number">58</span> <span class="token number">2</span>月  <span class="token number">19</span> 01:50 control
lrwxrwxrwx. <span class="token number">1</span> root root      <span class="token number">7</span> <span class="token number">2</span>月  <span class="token number">19</span> 01:50 VolGroup-LogVol00 -<span class="token operator">></span> <span class="token punctuation">..</span>/dm-1
lrwxrwxrwx. <span class="token number">1</span> root root      <span class="token number">7</span> <span class="token number">2</span>月  <span class="token number">19</span> 01:50 VolGroup-LogVol01 -<span class="token operator">></span> <span class="token punctuation">..</span>/dm-0
lrwxrwxrwx. <span class="token number">1</span> root root      <span class="token number">7</span> <span class="token number">2</span>月  <span class="token number">19</span> 01:50 VolGroup-LogVol02 -<span class="token operator">></span> <span class="token punctuation">..</span>/dm-2</code></pre>
<h4 id="使用fdisk分区">使用fdisk分区</h4>
<ul>
<li>硬盘分区</li>
</ul>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># fdisk /dev/sdb </span>
Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: n
Command action
   e   extended
   p   primary partition <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>
p
Partition number <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>: <span class="token number">1</span>
First cylinder <span class="token punctuation">(</span><span class="token number">1</span>-2610, default <span class="token number">1</span><span class="token punctuation">)</span>: 
Using default value <span class="token number">1</span>
Last cylinder, +cylinders or +size<span class="token punctuation">&#123;</span>K,M,G<span class="token punctuation">&#125;</span> <span class="token punctuation">(</span><span class="token number">1</span>-2610, default <span class="token number">2610</span><span class="token punctuation">)</span>: +2G

Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: n        
Command action
   e   extended
   p   primary partition <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>
p
Partition number <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>: <span class="token number">2</span>
First cylinder <span class="token punctuation">(</span><span class="token number">263</span>-2610, default <span class="token number">263</span><span class="token punctuation">)</span>: 
Using default value <span class="token number">263</span>
Last cylinder, +cylinders or +size<span class="token punctuation">&#123;</span>K,M,G<span class="token punctuation">&#125;</span> <span class="token punctuation">(</span><span class="token number">263</span>-2610, default <span class="token number">2610</span><span class="token punctuation">)</span>: +2G

Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: n
Command action
   e   extended
   p   primary partition <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>
p
Partition number <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>: <span class="token number">3</span>
First cylinder <span class="token punctuation">(</span><span class="token number">525</span>-2610, default <span class="token number">525</span><span class="token punctuation">)</span>: 
Using default value <span class="token number">525</span>
Last cylinder, +cylinders or +size<span class="token punctuation">&#123;</span>K,M,G<span class="token punctuation">&#125;</span> <span class="token punctuation">(</span><span class="token number">525</span>-2610, default <span class="token number">2610</span><span class="token punctuation">)</span>: +2G

Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: p					<span class="token comment"># 创建了三个标准分区</span>

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               <span class="token number">1</span>         <span class="token number">262</span>     <span class="token number">2104483</span>+  <span class="token number">83</span>  Linux
/dev/sdb2             <span class="token number">263</span>         <span class="token number">524</span>     <span class="token number">2104515</span>   <span class="token number">83</span>  Linux
/dev/sdb3             <span class="token number">525</span>         <span class="token number">786</span>     <span class="token number">2104515</span>   <span class="token number">83</span>  Linux

Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: t					<span class="token comment"># 修改分区类型</span>
Partition number <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>: <span class="token number">1</span>
Hex code <span class="token punctuation">(</span>type L to list codes<span class="token punctuation">)</span>: 8e		 <span class="token comment"># 8e是LVM</span>
Changed system <span class="token builtin class-name">type</span> of partition <span class="token number">1</span> to 8e <span class="token punctuation">(</span>Linux LVM<span class="token punctuation">)</span>

Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: t
Partition number <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>: <span class="token number">2</span>
Hex code <span class="token punctuation">(</span>type L to list codes<span class="token punctuation">)</span>: 8e
Changed system <span class="token builtin class-name">type</span> of partition <span class="token number">2</span> to 8e <span class="token punctuation">(</span>Linux LVM<span class="token punctuation">)</span>

Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: t
Partition number <span class="token punctuation">(</span><span class="token number">1</span>-4<span class="token punctuation">)</span>: <span class="token number">3</span>
Hex code <span class="token punctuation">(</span>type L to list codes<span class="token punctuation">)</span>: 8e
Changed system <span class="token builtin class-name">type</span> of partition <span class="token number">3</span> to 8e <span class="token punctuation">(</span>Linux LVM<span class="token punctuation">)</span>

Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: p					<span class="token comment"># 打印可以看到Linux变为了Linux LVM</span>

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               <span class="token number">1</span>         <span class="token number">262</span>     <span class="token number">2104483</span>+  8e  Linux LVM
/dev/sdb2             <span class="token number">263</span>         <span class="token number">524</span>     <span class="token number">2104515</span>   8e  Linux LVM
/dev/sdb3             <span class="token number">525</span>         <span class="token number">786</span>     <span class="token number">2104515</span>   8e  Linux LVM

Command <span class="token punctuation">(</span>m <span class="token keyword">for</span> <span class="token builtin class-name">help</span><span class="token punctuation">)</span>: w					<span class="token comment"># 写入</span></code></pre>
<ul>
<li><p>建立PV</p>
<p><code>pvcreat</code>可以建立pv，注意设备名</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pvcreate /dev/sdb1</span>
  Physical volume <span class="token string">"/dev/sdb1"</span> successfully created
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pvcreate /dev/sdb2</span>
  Physical volume <span class="token string">"/dev/sdb2"</span> successfully created
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pvcreate /dev/sdb3</span>
  Physical volume <span class="token string">"/dev/sdb3"</span> successfully created</code></pre></li>
<li><p>查看PV</p>
<p>可以使用<code>pvscan</code>,<code>pvdisplay</code>查看简略/详细的pv</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pvscan</span>
  PV /dev/sdb1   VG myvg1           lvm2 <span class="token punctuation">[</span><span class="token number">2.00</span> GiB / <span class="token number">2.00</span> GiB free<span class="token punctuation">]</span>
  PV /dev/sdb2   VG myvg1           lvm2 <span class="token punctuation">[</span><span class="token number">2.00</span> GiB / <span class="token number">2.00</span> GiB free<span class="token punctuation">]</span>
  PV /dev/sdb3                      lvm2 <span class="token punctuation">[</span><span class="token number">2.01</span> GiB<span class="token punctuation">]</span>
  Total: <span class="token number">3</span> <span class="token punctuation">[</span><span class="token number">6.01</span> GiB<span class="token punctuation">]</span> / <span class="token keyword">in</span> use: <span class="token number">2</span> <span class="token punctuation">[</span><span class="token number">4.01</span> GiB<span class="token punctuation">]</span> / <span class="token keyword">in</span> no VG: <span class="token number">1</span> <span class="token punctuation">[</span><span class="token number">2.01</span> GiB<span class="token punctuation">]</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pvdisplay </span>
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               myvg1
  PV Size               <span class="token number">2.01</span> GiB / not usable <span class="token number">3.16</span> MiB
  Allocatable           <span class="token function">yes</span> 
  PE Size               <span class="token number">4.00</span> MiB
  Total PE              <span class="token number">513</span>
  Free PE               <span class="token number">513</span>
  Allocated PE          <span class="token number">0</span>
  PV UUID               GdSlLe-Ihf5-JNcy-ODaq-Fy8y-VW8k-Ve7wnd
   
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               myvg1
  PV Size               <span class="token number">2.01</span> GiB / not usable <span class="token number">3.19</span> MiB
  Allocatable           <span class="token function">yes</span> 
  PE Size               <span class="token number">4.00</span> MiB
  Total PE              <span class="token number">513</span>
  Free PE               <span class="token number">513</span>
  Allocated PE          <span class="token number">0</span>
  PV UUID               G5oqcJ-RLNx-yHR2-CB3s-KfSn-ftIP-jI0CvH
   
  <span class="token string">"/dev/sdb3"</span> is a new physical volume of <span class="token string">"2.01 GiB"</span>
  --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name               
  PV Size               <span class="token number">2.01</span> GiB
  Allocatable           NO
  PE Size               <span class="token number">0</span>   
  Total PE              <span class="token number">0</span>
  Free PE               <span class="token number">0</span>
  Allocated PE          <span class="token number">0</span>
  PV UUID               PqTUdA-2VYn-I3wE-rVBY-7Mja-e2GR-sPR3qz</code></pre></li>
<li><p>建立VG</p>
<p><code>vgcreat VG名 PV设备们</code></p>
<ul>
<li><code>-s</code> 指定PE大小，没人用</li>
</ul>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># vgcreate myvg1 /dev/sdb1 /dev/sdb2		# 将两个PV加入VG</span>
  Volume group <span class="token string">"myvg1"</span> successfully created</code></pre></li>
<li><p>查看VG</p>
<p>可以使用<code>vgscan</code>,<code>vgdisplay</code>查看简略/详细的vg</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># vgscan</span>
  Reading all physical volumes.  This may take a while<span class="token punctuation">..</span>.
  Found volume group <span class="token string">"myvg1"</span> using metadata <span class="token builtin class-name">type</span> lvm2
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># vgdisplay </span>
  --- Volume group ---
  VG Name               myvg1
  System ID             
  Format                lvm2
  Metadata Areas        <span class="token number">2</span>
  Metadata Sequence No  <span class="token number">1</span>
  VG Access             read/write
  VG Status             resizable
  MAX LV                <span class="token number">0</span>
  Cur LV                <span class="token number">0</span>
  Open LV               <span class="token number">0</span>
  Max PV                <span class="token number">0</span>
  Cur PV                <span class="token number">2</span>
  Act PV                <span class="token number">2</span>
  VG Size               <span class="token number">4.01</span> GiB
  PE Size               <span class="token number">4.00</span> MiB
  Total PE              <span class="token number">1026</span>
  Alloc PE / Size       <span class="token number">0</span> / <span class="token number">0</span>   
  Free  PE / Size       <span class="token number">1026</span> / <span class="token number">4.01</span> GiB
  VG UUID               zn4VDW-Q5nz-ctFv-71E3-m3oo-OIy9-tyyyrE</code></pre></li>
<li><p>VG扩容</p>
<p><code>vgextend VG名 PV设备</code>可以扩容</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># vgextend myvg1 /dev/sdb3</span>
  Volume group <span class="token string">"myvg1"</span> successfully extended</code></pre>
<p>扩容无需重挂载，可以在占用的时候扩容</p></li>
<li><p>VG缩容</p>
<p>禁止操作</p></li>
<li><p>建立LV</p>
<p><code>lvcreat 卷组名</code>可以建立LV</p>
<ul>
<li><code>-L 容量</code>：指定LV大小，单位有M<strong>B</strong>，G<strong>B</strong>，T<strong>B</strong>...，注意写GB而非G</li>
<li><code>-l 个数</code>：指定LV大小，方式是有多少个PE，不好算，没人用</li>
<li><code>-n [LVName]</code>：指定逻辑卷名</li>
</ul>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># lvcreate -L 1GB -n mylv1 myvg1</span>
  Logical volume <span class="token string">"mylv1"</span> created.
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># lvcreate -L 2GB -n mylv2 myvg1</span>
  Logical volume <span class="token string">"mylv2"</span> created.
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># lvcreate -L 3GB -n mylv3 myvg1</span>
  Logical volume <span class="token string">"mylv3"</span> created.</code></pre></li>
<li><p>查看LV</p>
<p>可以使用<code>lvscan</code>,<code>lvdisplay</code>查看简略/详细的lv</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># lvscan</span>
  ACTIVE            <span class="token string">'/dev/myvg1/mylv1'</span> <span class="token punctuation">[</span><span class="token number">1.00</span> GiB<span class="token punctuation">]</span> inherit
  ACTIVE            <span class="token string">'/dev/myvg1/mylv2'</span> <span class="token punctuation">[</span><span class="token number">2.00</span> GiB<span class="token punctuation">]</span> inherit
  ACTIVE            <span class="token string">'/dev/myvg1/mylv3'</span> <span class="token punctuation">[</span><span class="token number">3.00</span> GiB<span class="token punctuation">]</span> inherit
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># lvdisplay </span>
  --- Logical volume ---
  LV Path                /dev/myvg1/mylv1
  LV Name                mylv1
  VG Name                myvg1
  LV UUID                DP4H2E-y372-VzQu-KSIE-35tR-ipJT-w5GEqT
  LV Write Access        read/write
  LV Creation host, <span class="token function">time</span> <span class="token number">10.0</span>.2.15, <span class="token number">2021</span>-02-19 02:23:04 +0800
  LV Status              available
  <span class="token comment"># open                 0</span>
  LV Size                <span class="token number">1.00</span> GiB
  Current LE             <span class="token number">256</span>
  Segments               <span class="token number">1</span>
  Allocation             inherit
  Read ahead sectors     auto
  - currently <span class="token builtin class-name">set</span> to     <span class="token number">256</span>
  Block device           <span class="token number">253</span>:0
   
  --- Logical volume ---
  LV Path                /dev/myvg1/mylv2
  LV Name                mylv2
  VG Name                myvg1
  LV UUID                khVCFL-addO-mnzy-6CwG-TtKd-T8Nh-kJGdAC
  LV Write Access        read/write
  LV Creation host, <span class="token function">time</span> <span class="token number">10.0</span>.2.15, <span class="token number">2021</span>-02-19 02:23:16 +0800
  LV Status              available
  <span class="token comment"># open                 0</span>
  LV Size                <span class="token number">2.00</span> GiB
  Current LE             <span class="token number">512</span>
  Segments               <span class="token number">1</span>
  Allocation             inherit
  Read ahead sectors     auto
  - currently <span class="token builtin class-name">set</span> to     <span class="token number">256</span>
  Block device           <span class="token number">253</span>:1
   
  --- Logical volume ---
  LV Path                /dev/myvg1/mylv3
  LV Name                mylv3
  VG Name                myvg1
  LV UUID                ypbyjM-ClmO-6fzL-oa7e-kREr-qxES-mPQe1N
  LV Write Access        read/write
  LV Creation host, <span class="token function">time</span> <span class="token number">10.0</span>.2.15, <span class="token number">2021</span>-02-19 02:23:21 +0800
  LV Status              available
  <span class="token comment"># open                 0</span>
  LV Size                <span class="token number">3.00</span> GiB
  Current LE             <span class="token number">768</span>
  Segments               <span class="token number">2</span>
  Allocation             inherit
  Read ahead sectors     auto
  - currently <span class="token builtin class-name">set</span> to     <span class="token number">256</span>
  Block device           <span class="token number">253</span>:2</code></pre></li>
<li><p>格式化LV</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># mkfs -t ext4 /dev/myvg1/mylv1</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># mkfs -t ext4 /dev/myvg1/mylv2</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># mkfs -t ext4 /dev/myvg1/mylv3</span></code></pre>
<p>注意新分区的位置在<code>dev/[vgname]/[lvname]</code>，这里的<code>mylv1</code>，<code>mylv2</code>，<code>mylv3</code>只不过是因为当时恰好这么起名的，不必后面是1,2,3</p></li>
<li><p>LV扩容</p>
<p><code>lvresize LV设备名</code>调整大小</p>
<ul>
<li><code>-L 容量</code>：修改大小，可以接+2GB,-1GB调整增量，也可以直接5GB调整到5GB，单位有M<strong>B</strong>，G<strong>B</strong>，T<strong>B</strong>...，注意写GB而非G</li>
<li><code>-l 个数</code>：修改大小，加上调整后的PE数</li>
</ul>
<p><code>resize2fs LV设备名</code>刷新大小</p>
<p>扩容无需卸载，可以在占用的时候扩容</p></li>
<li><p>挂载</p>
<p>略</p></li>
</ul>
<h2 id="shell基础">Shell基础</h2>
<p>Shell编程的目的是方便运维人员的工作，为了<strong>自己使用</strong>，对<strong>效率要求不高</strong>，<strong>思路简单</strong>，代码长，不像其他编程语言追求精简的代码，复杂的思路</p>
<p>### Shell概述</p>
<ul>
<li>Shell是一个命令解释器，将命令翻译成机器可以看懂的二进制指令</li>
<li>Linux使用的Shell名字叫做Bash，可以在<code>/etc/shell</code>查询Linux支持的shell</li>
</ul>
<h3 id="shell的执行方式">Shell的执行方式</h3>
<ul>
<li><p><code>echo</code>命令</p>
<p>输出指定内容</p>
<ul>
<li><p>对于没有空格的文本可以直接写</p></li>
<li><p>有空格的需要在文本两边加上""</p></li>
<li><p><code>-n</code>输出后不换行，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo 123</span>
<span class="token number">123</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo -n 123</span>
<span class="token number">123</span><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># </span></code></pre></li>
<li><p><code>-e</code>支持输出转义字符</p></li>
<li><p><code>echo -e "\e[1;XXmabcd\e[0m"</code>彩色输出abcd，其中<code>\e[1;XXm</code>和<code>\e[0m</code>相当于左右括号前后无需空格，XX内填写的数字表示颜色，第一位3表示修改字体颜色，4代表背景色，第二位0黑，1红，2绿，3黄，4蓝，5洋红，6青色，7白色</p></li>
<li><p>shell想要输出<code>!</code>必须写成<code>!空格</code>否则报错，显示的时候也会显示空格</p></li>
</ul></li>
<li><p>shell脚本最好以.sh结尾</p></li>
<li><p>第一句必须是<code>#!/bin/bash</code>这个不是注释，叫做bashbang，在多语言的脚本中没他跑不起来</p></li>
<li><p>执行</p>
<ul>
<li>直接文件名可以执行，但是要赋予执行权限</li>
<li><code>bash 文件名</code>可以执行，不需要x权限，但是没人用...</li>
</ul></li>
</ul>
<h3 id="shell的基本功能">Shell的基本功能</h3>
<ul>
<li><p>查看历史命令</p>
<ul>
<li><p>可以使用上箭头显示历史命令/<code>history</code>查询历史命令</p></li>
<li><p>系统会将历史命令记录在<code>~/.bash_history</code>下，history就是输出的这个文件</p></li>
<li><p><code>~/.bash_history</code>文件只有在系统关机的时候才会写入，也就是说不会显示本次登陆后写的命令，可以使用<code>history -w</code>立刻写入本次登录后的历史命令</p></li>
<li><p>在<code>/etc/profile</code>修改历史命令记录条数<code>HISTSIZE=1000</code></p></li>
<li><p>可以使用<code>history -c</code>清除本次登录后和<code>~/.bash_history</code>的历史记录，上箭头也失效，一般用于输入明文密码后</p></li>
<li><p>可以在<code>history</code>之后使用<code>!数字</code>执行history显示的第某编号条命令</p></li>
<li><p><code>!!</code>执行上一条命令</p></li>
<li><p><code>!XXX</code>执行以XXX开头的最后一条命令</p></li>
<li><p><code>!$</code>可以被替换为上一个命令的最后一个参数，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># touch aaa</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># ls</span>
aaa  anaconda-ks.cfg  install.log  install.log.syslog
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># rm -rf aaa</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># touch !$</span>
<span class="token function">touch</span> aaa
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># ls</span>
aaa  anaconda-ks.cfg  install.log  install.log.syslog</code></pre></li>
</ul></li>
<li><p>Tab补全</p></li>
<li><p>别名</p>
<ul>
<li>使用<code>alias 新命令="原来命令"</code>临时生效，注意前面没有""，后面有""</li>
<li>在<code>~/.bashrc</code>中直接写下临时命令即可永久生效，重启或者<code>source ~/.bashrc</code>即可启用</li>
<li>Bash中命令的执行优先级
<ul>
<li>绝对路径</li>
<li>别名</li>
<li>Bash命令</li>
<li>在PATH环境变量中找到的<strong>第一个</strong>命令</li>
</ul></li>
</ul></li>
<li><p><code>.</code></p>
<ul>
<li><code>.</code>相当于source的别名，但是别名里面查询不到</li>
<li><code>./</code>是当前目录</li>
<li><code>.XXX</code>是隐藏文件</li>
</ul></li>
<li><p>常见快捷键</p>
<ul>
<li><p><code>Ctrl+A</code>光标移动到开头</p></li>
<li><p><code>Ctrl+E</code>光标移动到末尾</p></li>
<li><p><code>Ctrl+C</code>终止</p></li>
<li><p><code>Ctrl+L</code>清屏</p></li>
<li><p><code>Ctrl+U</code>剪切光标之前的内容</p></li>
<li><p><code>Ctrl+K</code>剪切光标之后的命令</p></li>
<li><p><code>Ctrl+Y</code>粘贴剪切的内容</p></li>
<li><p><code>Ctrl+R</code>在历史命令中搜索</p></li>
<li><p><code>Ctrl+D</code>退出终端</p></li>
<li><p><code>Ctrl+Z</code>将程序暂停然后存入后台(注意不要当成<code>Ctrl+C</code>用)</p></li>
<li><p><code>Ctrl+S</code>暂停屏幕输出</p></li>
<li><p><code>Ctrl+Q</code>恢复屏幕输出</p></li>
</ul></li>
<li><p>IO重定向</p>
<ul>
<li><p>Bash下的标准IO有</p>
<table>
<thead>
<tr class="header">
<th>设备</th>
<th>文件名</th>
<th>文件描述符</th>
<th>类型</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>键盘</td>
<td>/dev/stdin</td>
<td>0</td>
<td>标准输入</td>
</tr>
<tr class="even">
<td>显示器</td>
<td>/dev/stdout</td>
<td>1</td>
<td>标准输出</td>
</tr>
<tr class="odd">
<td>显示器</td>
<td>/dev/stderr</td>
<td>2</td>
<td>标准错误输出</td>
</tr>
</tbody>
</table></li>
<li><p>输出重定向</p>
<p>命令的输出本来应该输出到标准输出，但是我们改变了输出的方向，输出到了文件</p>
<ul>
<li><p>标准输出的重定向</p>
<p>可以将命令的正确输出输出到指定的文件，方式是<code>命令 &gt; 文件</code>，或者<code>命令 &gt;&gt; 文件</code>，区别是，前者是覆盖文件内容，后者是追加</p></li>
<li><p>标准错误输出重定向</p>
<p>可以将命令的报错信息重定向到文件，但是正确的输出不会重定向，方法是<code>错误命令 2&gt; 文件</code>或者<code>错误命令 2&gt;&gt; 文件</code></p></li>
<li><p>同时输出正确与错误的输出</p>
<ul>
<li><code>命令 &gt;&gt; 文件1 2&gt;&gt; 文件2</code>1,2分别存放正确和错误输出</li>
<li><code>命令 &amp;&gt;&gt; 文件</code>或者<code>命令 &gt;&gt; 文件 2&gt;&amp;1</code>两者全部追加</li>
<li><code>命令 &amp;&gt; 文件</code>或者<code>命令 &gt; 文件 2&gt;&amp;1</code>两者全部覆盖</li>
</ul></li>
</ul></li>
<li><p>输入重定向</p>
<p>改变输入方式，变为从文件输入</p>
<ul>
<li><p><code>&lt;</code>是直接文件输入，例如<code>patch -p3 &lt; txt.patch</code></p></li>
<li><p><code>&lt;&lt;</code>是输入部分内容，当出现与开头相同的内容的时候将中间的内容输入，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># cat &lt;&lt; Imhead</span>
<span class="token operator">></span> as
<span class="token operator">></span> sd
<span class="token operator">></span> <span class="token function">df</span>
<span class="token operator">></span> Imhead
as
sd
<span class="token function">df</span></code></pre></li>
</ul></li>
</ul></li>
<li><p>多命令顺序执行</p>
<p>可以在一行下写下多个命令，让这些命令顺序执行，同时这几条命令不像管道一样结果传递，命令之间完全平行，中间可以使用不同的符号隔开</p>
<ul>
<li><code>;</code>：命令之间完全无关，一条执行完一条执行，不论前一条是否报错</li>
<li><code>&amp;&amp;</code>：只有前一条命令正确才执行后一条</li>
<li><code>||</code>：只有前一条错误才执行后一条</li>
</ul>
<p>可以理解成C++中的<code>;</code>,<code>&amp;&amp;</code>,<code>||</code>，且存在短路操作</p></li>
<li><p>管道符</p></li>
<li><p>同配符</p></li>
<li><p>Bash中的特殊符号</p>
<ul>
<li><code>''</code>：单引号表示其中的特殊内容没有特殊含义，例如‘$name’就表示字符串而不是变量值</li>
<li><code>""</code>：双引号表示其中的特殊内容没有特殊含义，"$","`",""除外<br />
</li>
<li><code>``</code>：用来<strong>引用系统命令的结果</strong>，括起来的内容是系统命令，Bash会优先执行，与$()一样，但是不推荐，容易看成‘</li>
<li><code>$()</code>：用来<strong>引用系统命令的结果</strong>，括起来的内容是系统命令，Bash会优先执行，例如<code>s=$(date)</code>得到的s就是当前时间的字符串</li>
<li><code>()</code>：括起来的是系统命令，会在<strong>子shell</strong>中执行括起来的命令，命令必须以;结尾，最后一个可以不加</li>
<li><code>&#123;&#125;</code>：括起来的是系统命令，会在<strong>父shell</strong>中执行括起来的命令，命令必须以;结尾<br />
</li>
<li><code>[]</code>：用于变量测试</li>
<li><code>#</code>：注释<br />
</li>
<li><code>$</code>：调用变量的时候需要，例如<span
class="math inline">\(name相当于是name变量，但是name相当于是字符串，在定义的时候不需要\)</span>,直接name=1即可</li>
<li><code>\</code>：转义符</li>
</ul></li>
<li><p>父Shell与子Shell</p>
<p>我们在开一个Shell之后还可以在他的内部再开一个Shell,例如：</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pstree									</span>
init─┬─省略一些进程
     ├─省略一些进程
     ├─sshd───sshd───bash───pstree
     └─省略一些进程
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># bash</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pstree</span>
init─┬─省略一些进程
     ├─省略一些进程<span class="token punctuation">..</span>.
     ├─sshd───sshd───bash───bash───pstree
     └─省略一些进程</code></pre>
<p>最开始使用pstree查看进程树看到有一个bash下开了一个pstree，在我们输入bash后查进程树看到一个bash下开了一个bash下有个pstree，相当于我们处于资格bash下的bash，这个bash就是原bash的子bash，在这个Bash下原Bash的变量等等都是隔离的，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># a=1</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># (a=2;echo $a;)</span>
<span class="token number">2</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $a</span>
<span class="token number">1</span></code></pre>
<p>这个和C++的变量保护一样，子有用子，子无用父</p></li>
</ul>
<h3 id="shell的变量和运算符">Shell的变量和运算符</h3>
<h4 id="变量">变量</h4>
<ul>
<li><p>变量规则</p>
<ul>
<li><p>命名规则于从C++ 相同</p></li>
<li><p>弱类型，但是特殊规定，默认是字符串型，<strong>如果要进行数值运算必须指定为数值类型</strong></p></li>
<li><p>变量赋值的时候<code>=</code>两边左右不得加空格</p></li>
<li><p>变量允许叠加，例如</p>
<p><pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># test=123</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># test="$test"456</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $test</span>
<span class="token number">123456</span></code></pre></p></li>
<li><p>环境变量建议全大写</p></li>
</ul></li>
<li><p>变量分类</p>
<p>Shell只有字符串和数值类型，这里是按照操作环境分类</p>
<ul>
<li>用户自定义变量：由用户自定义的</li>
<li>环境变量：保存于系统相关的数据，例如<code>/etc/profile</code>的HISTSIZE，可以分成两种，系统自带的环境变量，名字，作用是确定的，值可以自定义，例如分辨率。用户自定义的环境变量最大的作用就是可以跨Shell调用，全局生效。建议将环境变量全大写方便区分</li>
<li>位置参数变量：向脚本传递参数</li>
<li>预定义变量：Bash中定义好的变量</li>
</ul></li>
<li><p>用户自定义变量</p>
<ul>
<li><p>定义：<code>name=123</code></p></li>
<li><p>调用：<code>echo $name</code>(echo一个不存在的变量结果是空)</p></li>
<li><p>查看所有变量：<code>set [选项] 后面没有操作对象</code></p>
<ul>
<li><code>-u</code> 调用未声明的变量会报错(正常情况为空)</li>
<li><code>-x</code> 在执行命令之前会重新输出命令</li>
<li><code>空</code> 显示所有变量</li>
</ul>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># a=1					# 在Bash中我只定义了a不定义b</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $a				# 正确输出a</span>
<span class="token number">1</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $b				# 未知内容和空内容输出相同</span>

<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo ""</span>

<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># c="$a"0				# 正常拼接</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $c</span>
<span class="token number">10</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># c="$b"0				# 未知变量拼接不报错，只不过只有0</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $c</span>
<span class="token number">0</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># set -u					# 开启-u</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $a				# 已经定义的正常</span>
<span class="token number">1</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $b				# 未定义的报错</span>
-bash: b: unbound variable
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># c="$a"0				# 已定义的正常</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $c</span>
<span class="token number">10</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># c="$b"0				# 未定义的报错</span>
-bash: b: unbound variable
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $c				# 还是报错前的状态</span>
<span class="token number">10</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># set -x					# 开启-x</span>
++ <span class="token builtin class-name">printf</span> <span class="token string">'\033]0;%s@%s:%s\007'</span> root <span class="token number">10</span> <span class="token string">'~'</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo hello</span>
+ <span class="token builtin class-name">echo</span> hello
hello
++ <span class="token builtin class-name">printf</span> <span class="token string">'\033]0;%s@%s:%s\007'</span> root <span class="token number">10</span> <span class="token string">'~'</span></code></pre></li>
<li><p>删除变量</p>
<p><code>unset 变量</code>不需要加<code>$</code></p></li>
</ul></li>
<li><p>环境变量</p>
<p>用户可以自定义环境变量，于用户自定义变量不同的是环境变量可以在子shell生效，全局生效</p>
<ul>
<li><p>声明：<code>export name=18</code>，或者<code>export name</code>将已经定义的用户自定义变量提升为环境变量</p></li>
<li><p>查询所有环境变量：</p>
<p><code>set</code>可以看到部分环境变量和用户自定义变量，<code>env</code>可以看到部分环境变量，但是都不全，有重叠</p></li>
<li><p>删除自定义变量<code>unset 变量</code></p></li>
<li><p>重要的系统自带环境变量</p>
<ul>
<li><p><code>$PATH</code>：用:分割开的路径，是系统查找命令的路径</p>
<p>我们通过实现“不写路径执行shell”实验证明</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $PATH					# 先查询一下PATH</span>
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo "echo Success" >> ./pathTest.sh	# 随便放个sh</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># chmod 777 pathTest.sh 					# 改为可执行</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pathTest.sh							# 试着运行</span>
-bash: pathTest.sh: <span class="token builtin class-name">command</span> not found
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># cp pathTest.sh /bin/					# 放到$PATH的一个路径下</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pathTest.sh							# 试着运行通过</span>
Success
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># rm -rf /bin/pathTest.sh 				# 删除</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pathTest.sh							# 试着运行</span>
-bash: /bin/pathTest.sh: 没有那个文件或目录
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># PATH="$PATH":/root						# 将当前目录加入$PATH</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $PATH								# 临时更新成功</span>
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># pathTest.sh							# 试着运行通过</span>
Success
</code></pre></li>
<li><p><code>$PS1</code>：命令提示符格式</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $PS1</span>
<span class="token punctuation">[</span><span class="token punctuation">\</span>u@<span class="token punctuation">\</span>h <span class="token punctuation">\</span>W<span class="token punctuation">]</span><span class="token punctuation">\</span>$</code></pre>
<p>其中</p>
<ul>
<li><code>\d</code> ：代表日期，格式为weekday month date，例如："Mon Aug
1"</li>
<li><code>\H</code>
：完整的主机名称。例如：我的机器名称为：fc4.linux，则这个名称就是fc4.linux</li>
<li><code>\h</code>
：仅取主机的第一个名字，如上例，则为fc4，.linux则被省略</li>
<li><code>\t</code> ：显示时间为24小时格式，如：HH：MM：SS</li>
<li><code>\T</code> ：显示时间为12小时格式</li>
<li><code>\A</code> ：显示时间为24小时格式：HH：MM</li>
<li><code>\u</code> ：当前用户的账号名称</li>
<li><code>\v</code> ：BASH的版本信息</li>
<li><code>\w</code> ：完整的工作目录名称。家目录会以 ~代替</li>
<li><code>\W</code>
：利用basename取得工作目录名称，所以只会列出最后一个目录</li>
<li><code>\#</code> ：下达的第几个命令</li>
<li><code>\$</code> ：提示字符，如果是root时，提示符为：#
，普通用户则为：$</li>
</ul></li>
<li><p><code>$LANG</code>：当前终端上的Linux语系</p></li>
</ul></li>
</ul></li>
<li><p>位置参数变量</p>
<ul>
<li><p><code>$n</code>：n是数字，$0是命令本身，<span
class="math inline">\(1-9是命令的第1-9个参数，10+要用大括号括起来，例如\)</span>{22}，这个东西是用在脚本里面的，例如我写一个实现加法功能的脚本plus.sh</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>
<span class="token assign-left variable">a</span><span class="token operator">=</span><span class="token variable">$1</span>			<span class="token comment"># 将第一个参数赋值到a</span>
<span class="token assign-left variable">b</span><span class="token operator">=</span><span class="token variable">$2</span>			<span class="token comment"># 将第二个参数赋值到b</span>
<span class="token builtin class-name">echo</span> <span class="token variable"><span class="token variable">$((</span>$a<span class="token operator">+</span>$b<span class="token variable">))</span></span>	<span class="token comment"># 输出a+b 这里要将需要进行数值运算的内容方放入$(())否则会得到字符串拼接的结果</span></code></pre>
<p>执行脚本</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># chmod 777 plus.sh </span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># ./plus.sh 5 6</span>
<span class="token number">11</span></code></pre>
<p>在Bash中传参的方式有很多，尤其是需要交互的，还有其他方式</p></li>
<li><p><code>$#</code>参数个数</p></li>
<li><p><code>$*</code>将参数整体作为一个字符串</p></li>
<li><p><code>$@</code>将参数分割开传入，相当于<code>$*</code>传入了一个python的string，<code>$@</code>相当于传入了python的list</p>
<p>可以验证，写下pro.sh</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token builtin class-name">echo</span> <span class="token string">"\<span class="token variable">$#</span> is <span class="token variable">$#</span>"</span>
<span class="token builtin class-name">echo</span> <span class="token string">"\<span class="token variable">$@</span> is <span class="token variable">$@</span>"</span>
<span class="token builtin class-name">echo</span> <span class="token string">"\<span class="token variable">$*</span> is <span class="token variable">$*</span>"</span>

<span class="token builtin class-name">echo</span> -e <span class="token string">"iterator \<span class="token variable">$@</span><span class="token entity" title="\n">\n</span>=========="</span>
<span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> <span class="token string">"<span class="token variable">$@</span>"</span>		<span class="token comment"># for的语法与py类似</span>
        <span class="token keyword">do</span>			<span class="token comment"># 相当于C的大括号</span>
                <span class="token builtin class-name">echo</span> <span class="token variable">$i</span>
        <span class="token keyword">done</span>		<span class="token comment"># 相当于C的大括号</span>

<span class="token builtin class-name">echo</span> -e <span class="token string">"iterator \<span class="token variable">$*</span><span class="token entity" title="\n">\n</span>=========="</span>
<span class="token keyword">for</span> <span class="token for-or-select variable">j</span> <span class="token keyword">in</span> <span class="token string">"<span class="token variable">$*</span>"</span>							
<span class="token comment"># 这里比较奇怪的是如果将$*先赋值后用赋值者迭代会出现$@效果，原因可能是在这里$*被认为是一个字符串了，但是替换后，由于原来就有空格，而Bash只有字符串和数字类型就被直接转化成有空格的字符串，也就是这里语法中的一列东西了</span>
        <span class="token keyword">do</span>
                <span class="token builtin class-name">echo</span> <span class="token variable">$j</span>
        <span class="token keyword">done</span></code></pre>
<p>执行</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># chmod 777 ./pro.sh</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># ./pro.sh aaa bbb ccc ddd eee</span>
<span class="token variable">$#</span> is <span class="token number">5</span>
<span class="token variable">$@</span> is aaa bbb ccc ddd eee
<span class="token variable">$*</span> is aaa bbb ccc ddd eee
iterator <span class="token variable">$@</span>
<span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span>
aaa
bbb
ccc
ddd
eee
iterator <span class="token variable">$*</span>
<span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span>
aaa bbb ccc ddd eee</code></pre></li>
</ul></li>
<li><p>预定义变量</p>
<ul>
<li><p><code>$?</code>：上一条命令的返回状态，0是正确执行，非0是错误，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo 123</span>
<span class="token number">123</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $?</span>
<span class="token number">0</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># das</span>
-bash: das: <span class="token builtin class-name">command</span> not found
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $?</span>
<span class="token number">127</span></code></pre></li>
<li><p><code>$$</code>当前PID</p></li>
<li><p><code>$!</code>后台最后一个进程的PID</p></li>
</ul></li>
<li><p>使用<code>read</code>接受键盘输入</p>
<p>read可以以交互的方式输入，<code>read [选项] 不加$的变量</code></p>
<ul>
<li><code>-t 秒数</code> ：超时时间</li>
<li><code>-p 字符串</code>：提示信息</li>
<li><code>-n 字数</code>：允许输入的字数</li>
<li><code>-s</code>：像输入密码一样，不显示内容</li>
</ul>
<p>重新实现加法器</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token builtin class-name">read</span> -t <span class="token number">30</span> -p <span class="token string">"input number 1: "</span> a
<span class="token builtin class-name">read</span> -t <span class="token number">30</span> -p <span class="token string">"input number 2: "</span> b

<span class="token builtin class-name">echo</span> <span class="token variable"><span class="token variable">$((</span>$a<span class="token operator">+</span>$b<span class="token variable">))</span></span></code></pre>
<p>执行</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># ./plus.sh </span>
input number <span class="token number">1</span>: <span class="token number">222</span>
input number <span class="token number">2</span>: <span class="token number">333</span>
<span class="token number">555</span></code></pre></li>
<li><p>使用<code>declear</code>声明变量类型</p>
<p><code>declear [+/-][选项] 变量名</code></p>
<ul>
<li><code>+</code>是去掉某类型，<code>-</code>是加上某类型</li>
<li><code>-a</code>声明成数组型</li>
<li><code>-i</code>声明成整数</li>
<li><code>-r</code>声明成常量，变成常量后不可通过<code>+r</code>改回，但是只读属性重启自动消失</li>
<li><code>-x</code>声明成环境变量</li>
<li><code>-p</code>输出变量类型</li>
</ul></li>
<li><p>数组</p>
<p>直接赋值，不要声明，支持跨下标存储，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># arr[0]=H</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># arr[2]=J</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># arr[4]=P</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $&#123;arr[2]&#125;		# 引用的时候要括起来否则相当于是$arr和[2]</span>
J
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $&#123;arr[*]&#125;		# 输出数组全部内容</span>
H J P
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $arr			# 只写名字会输出[0]</span>
H</code></pre></li>
</ul>
<h4 id="运算符">运算符</h4>
<ul>
<li><p>数值运算的方法</p>
<ul>
<li><p><code>declear</code>方法</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># a=1</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># b=2</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># declare -i c=$a+$b</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $c</span>
<span class="token number">3</span></code></pre></li>
<li><p><code>expr</code>方法</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># expr $a + $b</span>
<span class="token number">3</span></code></pre>
<p>如果需要赋值只需要<code>$(expr $a + $b)</code>注意expr的运算符两边<strong>必须加空格</strong></p></li>
<li><p><code>let</code>方法</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable">ll_form</span><span class="token operator">=</span><span class="token operator">></span>inputoutput: 打开注册页面，阅读<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># let d=$a+$b</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $d</span>
<span class="token number">3</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $(let $a+$b)</span>
</code></pre>
<p>这个不支持直接输出结果</p></li>
<li><p><code>$((表达式))</code>或者<code>$[表达式]</code>方法</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># echo $(($a+$b))</span>
<span class="token number">3</span></code></pre>
<p>强烈建议用<code>$((表达式))</code>，不推荐其他，不推荐<code>$[]</code></p></li>
</ul></li>
<li><p>变量测试与内容置换</p>
<table>
<colgroup>
<col style="width: 16%" />
<col style="width: 40%" />
<col style="width: 29%" />
<col style="width: 14%" />
</colgroup>
<thead>
<tr class="header">
<th>变量置换方式</th>
<th>变量y没有设置</th>
<th>变量y为空值</th>
<th>变量y设置值</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>x=<span class="math inline">\({y-新值} | x= 新值 | x 为空 |
x=\)</span>y</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="even">
<td>x=<span class="math inline">\({y:-新值} | x= 新值 | x= 新值 |
x=\)</span>y</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>x=<span class="math inline">\({y+新值} | x 为空 | x= 新值 | x=新值 |
| x=\)</span>{y:+新值}</td>
<td>x 为空</td>
<td>x 为空</td>
<td>x=新值</td>
</tr>
<tr class="even">
<td>x=<span class="math inline">\({y=新值} | x= 新值 | x 为空 |
x=\)</span>y</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>y= 新值</td>
<td>y 值不变</td>
<td>y值不变</td>
<td></td>
</tr>
<tr class="even">
<td>x=<span class="math inline">\({y:=新值} | x= 新值 | X= 新值 |
x=\)</span>y</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>y= 新值</td>
<td>y= 新值</td>
<td>y值不变</td>
<td></td>
</tr>
<tr class="even">
<td>x=<span class="math inline">\({y?新值} |
新值输出到标准错误输出（屏幕） | x 为空 | x=\)</span>y</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>x=<span class="math inline">\({y:?新值} | 新值输出到标准错误输出 |
新值输出到标准错误输出 | x=\)</span>y</td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>这个就是一套语法，一般只有第一个比较常用，用来检测y是不是空，其他的不强制要求记忆(毕竟shell为了方便自己而不是效率)</p></li>
</ul>
<h3 id="环境变量配置文件">环境变量配置文件</h3>
<ul>
<li><p><code>source</code>命令</p>
<p><code>source 配置文件</code>/<code>. 配置文件</code>使配置文件生效</p></li>
<li><p>常见环境变量配置文件</p>
<ul>
<li><p>在登录时候生效的(正常或非正常登录都算)</p>
<ul>
<li><code>/etc/profile</code></li>
<li><code>/etc/profile.d/*.sh</code></li>
<li><code>~/.bash_profile</code></li>
<li><code>~/.bashrc</code></li>
<li><code>/etc/bashrc</code></li>
</ul>
<p>在家目录下的只对当前用户生效</p>
<p>调用顺序</p>
<pre class="mermaid">    graph TB
登录{登录} --读取--> etcprofile(/etc/profile) --里面的脚本调用1--> etcpd(/etc/profile.d/*.sh) --主要是--> etclang(/etc/profile.d/lang.sh) --脚本调用--> etci18n(/etc/sysc.config/i18n) --return0--> etcprofile
etcprofile(/etc/profile) --里面的脚本调用2--> hpeo($home/.bash_peofile) --里面的脚本调用--> hbrc($HOME/.bashrc) --里面的脚本调用--> ebrc(/etc/bashrc) --里面的脚本调用--> 进入Bash
非登录{非登录} -.读取.-> ebrc -.里面的脚本调用1.-> etcpd -.主要是.-> etclang -.脚本调用.-> etci18n -.return0.-> ebrc
ebrc -.里面的脚本调用2.-> 进入Bash</pre>
<ul>
<li><p>登录与非登录的区别</p>
<p>登录指的是输入密码后登录，例如ssh远程到计算机，非登录指的是不需要输入密码的登录，例如在root下<code>su 其他用户</code>，在一个Bash里面输入Bash，可以进入子shell,这个时候环境变量生效顺序与父shell的不一样</p></li>
<li><p>用户登录后的调用顺序</p>
<ol type="1">
<li><p><code>/etc/profile</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function-name function">pathmunge</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>					<span class="token comment"># 定义了一个函数，后面加载，是用来定义PATH的</span>
    <span class="token keyword">case</span> <span class="token string">":<span class="token variable">$&#123;<span class="token environment constant">PATH</span>&#125;</span>:"</span> <span class="token keyword">in</span>
        *:<span class="token string">"<span class="token variable">$1</span>"</span>:*<span class="token punctuation">)</span>
            <span class="token punctuation">;</span><span class="token punctuation">;</span>
        *<span class="token punctuation">)</span>
            <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$2</span>"</span> <span class="token operator">=</span> <span class="token string">"after"</span> <span class="token punctuation">]</span> <span class="token punctuation">;</span> <span class="token keyword">then</span>
                <span class="token assign-left variable"><span class="token environment constant">PATH</span></span><span class="token operator">=</span><span class="token environment constant">$PATH</span><span class="token builtin class-name">:</span><span class="token variable">$1</span>
            <span class="token keyword">else</span>
                <span class="token assign-left variable"><span class="token environment constant">PATH</span></span><span class="token operator">=</span><span class="token variable">$1</span><span class="token builtin class-name">:</span><span class="token environment constant">$PATH</span>
            <span class="token keyword">fi</span>
    <span class="token keyword">esac</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> -x /usr/bin/id <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>		<span class="token comment"># 定义用户相关</span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span> -z <span class="token string">"<span class="token environment constant">$EUID</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
                <span class="token assign-left variable"><span class="token environment constant">EUID</span></span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">id</span> -u<span class="token variable">`</span></span>
        <span class="token assign-left variable"><span class="token environment constant">UID</span></span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">id</span> -ru<span class="token variable">`</span></span>
    <span class="token keyword">fi</span>
    <span class="token assign-left variable"><span class="token environment constant">USER</span></span><span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">`</span><span class="token function">id</span> -un<span class="token variable">`</span></span>"</span>					<span class="token comment"># 定义USER=当前用户</span>
    <span class="token assign-left variable"><span class="token environment constant">LOGNAME</span></span><span class="token operator">=</span><span class="token environment constant">$USER</span>					<span class="token comment"># 定义LOGNAME=USER</span>
    <span class="token assign-left variable">MAIL</span><span class="token operator">=</span><span class="token string">"/var/spool/mail/<span class="token environment constant">$USER</span>"</span>	<span class="token comment"># 定义邮箱地址</span>
<span class="token keyword">fi</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token environment constant">$EUID</span>"</span> <span class="token operator">=</span> <span class="token string">"0"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>			<span class="token comment"># 如果EUID是0(root)定义PATH(这个文件只加载一部分)</span>
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
<span class="token keyword">else</span>
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
    pathmunge /sbin after
<span class="token keyword">fi</span>
<span class="token assign-left variable"><span class="token environment constant">HOSTNAME</span></span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span>/bin/hostname <span class="token operator"><span class="token file-descriptor important">2</span>></span>/dev/null<span class="token variable">`</span></span>	<span class="token comment"># 主机名</span>
<span class="token assign-left variable"><span class="token environment constant">HISTSIZE</span></span><span class="token operator">=</span><span class="token number">10000</span>							<span class="token comment"># 历史命令条数</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token environment constant">$HISTCONTROL</span>"</span> <span class="token operator">=</span> <span class="token string">"ignorespace"</span> <span class="token punctuation">]</span> <span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token builtin class-name">export</span> <span class="token assign-left variable"><span class="token environment constant">HISTCONTROL</span></span><span class="token operator">=</span>ignoreboth
<span class="token keyword">else</span>
    <span class="token builtin class-name">export</span> <span class="token assign-left variable"><span class="token environment constant">HISTCONTROL</span></span><span class="token operator">=</span>ignoredups
<span class="token keyword">fi</span>
<span class="token builtin class-name">export</span> <span class="token environment constant">PATH</span> <span class="token environment constant">USER</span> <span class="token environment constant">LOGNAME</span> MAIL <span class="token environment constant">HOSTNAME</span> <span class="token environment constant">HISTSIZE</span> <span class="token environment constant">HISTCONTROL</span>
<span class="token comment"># 根据ID给umask权限</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token environment constant">$UID</span> -gt <span class="token number">199</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable"><span class="token variable">`</span><span class="token function">id</span> -gn<span class="token variable">`</span></span>"</span> <span class="token operator">=</span> <span class="token string">"<span class="token variable"><span class="token variable">`</span><span class="token function">id</span> -un<span class="token variable">`</span></span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token builtin class-name">umask</span> 002
<span class="token keyword">else</span>
    <span class="token builtin class-name">umask</span> 022
<span class="token keyword">fi</span>
<span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> /etc/profile.d/*.sh <span class="token punctuation">;</span> <span class="token keyword">do</span>		<span class="token comment"># 这里要加载/etc/profile.d/*.sh</span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span> -r <span class="token string">"<span class="token variable">$i</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$&#123;-<span class="token operator">#</span>*i&#125;</span>"</span> <span class="token operator">!=</span> <span class="token string">"$-"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token builtin class-name">.</span> <span class="token string">"<span class="token variable">$i</span>"</span>
        <span class="token keyword">else</span>
            <span class="token builtin class-name">.</span> <span class="token string">"<span class="token variable">$i</span>"</span> <span class="token operator">></span>/dev/null <span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token file-descriptor important">&amp;1</span>
        <span class="token keyword">fi</span>
    <span class="token keyword">fi</span>
<span class="token keyword">done</span>
<span class="token builtin class-name">unset</span> i
<span class="token builtin class-name">unset</span> -f pathmunge</code></pre></li>
<li><p><code>/etc/profild.d/*.sh</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># ll /etc/profile.d/*.sh</span>
-rw-r--r--. <span class="token number">1</span> root root <span class="token number">1179</span> <span class="token number">4</span>月  <span class="token number">12</span> <span class="token number">2016</span> /etc/profile.d/colorls.sh
-rw-r--r--. <span class="token number">1</span> root root   <span class="token number">78</span> <span class="token number">11</span>月 <span class="token number">22</span> <span class="token number">2013</span> /etc/profile.d/cvs.sh
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">192</span> <span class="token number">1</span>月  <span class="token number">21</span> <span class="token number">2016</span> /etc/profile.d/glib2.sh
-rw-r--r--. <span class="token number">1</span> root root <span class="token number">2706</span> <span class="token number">5</span>月  <span class="token number">12</span> <span class="token number">2016</span> /etc/profile.d/lang.sh
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">121</span> <span class="token number">6</span>月   <span class="token number">4</span> <span class="token number">2014</span> /etc/profile.d/less.sh
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">912</span> <span class="token number">9</span>月  <span class="token number">24</span> <span class="token number">2011</span> /etc/profile.d/qt.sh
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">269</span> <span class="token number">7</span>月  <span class="token number">24</span> <span class="token number">2015</span> /etc/profile.d/vim.sh
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">169</span> <span class="token number">5</span>月  <span class="token number">20</span> <span class="token number">2009</span> /etc/profile.d/which2.sh</code></pre>
<p>很多，但是很多都是三方的，真正有用的是<code>lang.sh</code></p></li>
<li><p><code>/etc/profile.d/lang.sh</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># /etc/profile.d/lang.sh - set i18n stuff</span>

<span class="token assign-left variable">sourced</span><span class="token operator">=</span><span class="token number">0</span>

<span class="token keyword">if</span> <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LANG</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token assign-left variable">saved_lang</span><span class="token operator">=</span><span class="token string">"<span class="token environment constant">$LANG</span>"</span>
    <span class="token punctuation">[</span> -f <span class="token string">"<span class="token environment constant">$HOME</span>/.i18n"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">.</span> <span class="token string">"<span class="token environment constant">$HOME</span>/.i18n"</span> <span class="token operator">&amp;&amp;</span> <span class="token assign-left variable">sourced</span><span class="token operator">=</span><span class="token number">1</span>
    <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span><span class="token string">"<span class="token variable">$saved_lang</span>"</span>
    <span class="token builtin class-name">unset</span> saved_lang
<span class="token keyword">else</span>
    <span class="token keyword">for</span> <span class="token for-or-select variable">langfile</span> <span class="token keyword">in</span> /etc/sysconfig/i18n <span class="token string">"<span class="token environment constant">$HOME</span>/.i18n"</span> <span class="token punctuation">;</span> <span class="token keyword">do</span>
    <span class="token comment"># 主要就是调用了这个 /etc/sysconfig/i18n</span>
        <span class="token punctuation">[</span> -f <span class="token variable">$langfile</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">.</span> <span class="token variable">$langfile</span> <span class="token operator">&amp;&amp;</span> <span class="token assign-left variable">sourced</span><span class="token operator">=</span><span class="token number">1</span>
    <span class="token keyword">done</span>
<span class="token keyword">fi</span>

<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$sourced</span>"</span> <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LANG</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LANG</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LANG</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_ADDRESS</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LC_ADDRESS</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_ADDRESS</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token variable">$LC_CTYPE</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> LC_CTYPE <span class="token operator">||</span> <span class="token builtin class-name">unset</span> LC_CTYPE
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token variable">$LC_COLLATE</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> LC_COLLATE <span class="token operator">||</span> <span class="token builtin class-name">unset</span> LC_COLLATE
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_IDENTIFICATION</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LC_IDENTIFICATION</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_IDENTIFICATION</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_MEASUREMENT</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LC_MEASUREMENT</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_MEASUREMENT</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token variable">$LC_MESSAGES</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> LC_MESSAGES <span class="token operator">||</span> <span class="token builtin class-name">unset</span> LC_MESSAGES
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_MONETARY</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LC_MONETARY</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_MONETARY</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_NAME</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LC_NAME</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_NAME</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_NUMERIC</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LC_NUMERIC</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_NUMERIC</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_PAPER</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LC_PAPER</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_PAPER</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_TELEPHONE</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LC_TELEPHONE</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_TELEPHONE</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_TIME</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LC_TIME</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_TIME</span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LC_ALL</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
       <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token environment constant">$LC_ALL</span>"</span> <span class="token operator">!=</span> <span class="token string">"<span class="token environment constant">$LANG</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
         <span class="token builtin class-name">export</span> <span class="token environment constant">LC_ALL</span>
       <span class="token keyword">else</span>
         <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_ALL</span>
       <span class="token keyword">fi</span>
    <span class="token keyword">else</span>
       <span class="token builtin class-name">unset</span> <span class="token environment constant">LC_ALL</span>
    <span class="token keyword">fi</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LANGUAGE</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> <span class="token environment constant">LANGUAGE</span> <span class="token operator">||</span> <span class="token builtin class-name">unset</span> <span class="token environment constant">LANGUAGE</span>
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token variable">$LINGUAS</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> LINGUAS <span class="token operator">||</span> <span class="token builtin class-name">unset</span> LINGUAS
    <span class="token punctuation">[</span> -n <span class="token string">"<span class="token variable">$_XKB_CHARSET</span>"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">export</span> _XKB_CHARSET <span class="token operator">||</span> <span class="token builtin class-name">unset</span> _XKB_CHARSET
    
    <span class="token assign-left variable">consoletype</span><span class="token operator">=</span><span class="token variable">$CONSOLETYPE</span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span> -z <span class="token string">"<span class="token variable">$consoletype</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
      <span class="token assign-left variable">consoletype</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span>/sbin/consoletype stdout<span class="token variable">)</span></span>
    <span class="token keyword">fi</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> -n <span class="token string">"<span class="token environment constant">$LANG</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
      <span class="token keyword">case</span> <span class="token environment constant">$LANG</span> <span class="token keyword">in</span>
    	*.utf8*<span class="token operator">|</span>*.UTF-8*<span class="token punctuation">)</span>
    	<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token environment constant">$TERM</span>"</span> <span class="token operator">=</span> <span class="token string">"linux"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    	    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$consoletype</span>"</span> <span class="token operator">=</span> <span class="token string">"vt"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    	    	<span class="token keyword">case</span> <span class="token environment constant">$LANG</span> <span class="token keyword">in</span> 
    	    		ja*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US.UTF-8 <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		ko*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US.UTF-8 <span class="token punctuation">;</span><span class="token punctuation">;</span>
			si*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US.UTF-8 <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		zh*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US.UTF-8 <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		ar*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US.UTF-8 <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		fa*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US.UTF-8 <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		he*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US.UTF-8 <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		en_IN*<span class="token punctuation">)</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		*_IN*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US.UTF-8 <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    	<span class="token keyword">esac</span>
            <span class="token keyword">fi</span>
        <span class="token keyword">fi</span>
	<span class="token punctuation">;</span><span class="token punctuation">;</span>
	*<span class="token punctuation">)</span>
	<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token environment constant">$TERM</span>"</span> <span class="token operator">=</span> <span class="token string">"linux"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
	    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$consoletype</span>"</span> <span class="token operator">=</span> <span class="token string">"vt"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    	    	<span class="token keyword">case</span> <span class="token environment constant">$LANG</span> <span class="token keyword">in</span> 
    	    		ja*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		ko*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US <span class="token punctuation">;</span><span class="token punctuation">;</span>
			si*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		zh*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		ar*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		fa*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		he*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		en_IN*<span class="token punctuation">)</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    		*_IN*<span class="token punctuation">)</span> <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>en_US <span class="token punctuation">;</span><span class="token punctuation">;</span>
    	    	<span class="token keyword">esac</span>
	    <span class="token keyword">fi</span>
	<span class="token keyword">fi</span>
	<span class="token punctuation">;</span><span class="token punctuation">;</span>
      <span class="token keyword">esac</span>
    <span class="token keyword">fi</span>

    <span class="token builtin class-name">unset</span> SYSFONTACM SYSFONT consoletype
<span class="token keyword">fi</span>
<span class="token builtin class-name">unset</span> sourced
<span class="token builtin class-name">unset</span> langfile</code></pre></li>
<li><p><code>/etc/sysconfig/i18n</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span><span class="token string">"zh_CN.UTF-8"</span></code></pre>
<p>保存的是当前的默认语言</p></li>
<li><p><code>~/.bashrc</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># .bash_profile</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> -f ~/.bashrc <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>		<span class="token comment"># 如果~/.bashrc存在就调用</span>
	<span class="token builtin class-name">.</span> ~/.bashrc
<span class="token keyword">fi</span>
<span class="token assign-left variable"><span class="token environment constant">PATH</span></span><span class="token operator">=</span><span class="token environment constant">$PATH</span><span class="token builtin class-name">:</span><span class="token environment constant">$HOME</span>/bin			<span class="token comment"># 加上环境变量</span>
<span class="token builtin class-name">export</span> <span class="token environment constant">PATH</span></code></pre></li>
<li><p><code>~/.bashrc</code></p>
<p>这里一般用来放用户自定义的别名和函数</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">if</span> <span class="token punctuation">[</span> -f /etc/bashrc <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>		<span class="token comment"># 如果~/.bashrc存在就调用</span>
	<span class="token builtin class-name">.</span> /etc/bashrc
<span class="token keyword">fi</span></code></pre></li>
<li><p><code>/etc/bashrc</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># /etc/bashrc</span>
<span class="token comment"># 定义了PS1</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token environment constant">$PS1</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
  <span class="token keyword">if</span> <span class="token punctuation">[</span> -z <span class="token string">"<span class="token variable">$PROMPT_COMMAND</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token keyword">case</span> <span class="token environment constant">$TERM</span> <span class="token keyword">in</span>
    xterm*<span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span> -e /etc/sysconfig/bash-prompt-xterm <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token assign-left variable">PROMPT_COMMAND</span><span class="token operator">=</span>/etc/sysconfig/bash-prompt-xterm
        <span class="token keyword">else</span>
            <span class="token assign-left variable">PROMPT_COMMAND</span><span class="token operator">=</span><span class="token string">'printf "\033]0;%s@%s:%s\007" "$&#123;USER&#125;" "$&#123;HOSTNAME%%.*&#125;" "$&#123;PWD/#$HOME/~&#125;"'</span>
        <span class="token keyword">fi</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
    <span class="token function">screen</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span> -e /etc/sysconfig/bash-prompt-screen <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token assign-left variable">PROMPT_COMMAND</span><span class="token operator">=</span>/etc/sysconfig/bash-prompt-screen
        <span class="token keyword">else</span>
            <span class="token assign-left variable">PROMPT_COMMAND</span><span class="token operator">=</span><span class="token string">'printf "\033]0;%s@%s:%s\033\\" "$&#123;USER&#125;" "$&#123;HOSTNAME%%.*&#125;" "$&#123;PWD/#$HOME/~&#125;"'</span>
        <span class="token keyword">fi</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
    *<span class="token punctuation">)</span>
        <span class="token punctuation">[</span> -e /etc/sysconfig/bash-prompt-default <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token assign-left variable">PROMPT_COMMAND</span><span class="token operator">=</span>/etc/sysconfig/bash-prompt-default
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
      <span class="token keyword">esac</span>
  <span class="token keyword">fi</span>
  <span class="token builtin class-name">shopt</span> -s checkwinsize
  <span class="token punctuation">[</span> <span class="token string">"<span class="token environment constant">$PS1</span>"</span> <span class="token operator">=</span> <span class="token string">"<span class="token entity" title="\\">\\</span>s-<span class="token entity" title="\\">\\</span>v<span class="token entity" title="\\">\\</span>\$ "</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token assign-left variable"><span class="token environment constant">PS1</span></span><span class="token operator">=</span><span class="token string">"[\u@\h \W]<span class="token entity" title="\\">\\</span>$ "</span>
<span class="token keyword">fi</span>

<span class="token comment"># 非登录执行的，见/etc/profile</span>
<span class="token keyword">if</span> <span class="token operator">!</span> <span class="token builtin class-name">shopt</span> -q login_shell <span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token function-name function">pathmunge</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">case</span> <span class="token string">":<span class="token variable">$&#123;<span class="token environment constant">PATH</span>&#125;</span>:"</span> <span class="token keyword">in</span>
            *:<span class="token string">"<span class="token variable">$1</span>"</span>:*<span class="token punctuation">)</span>
                <span class="token punctuation">;</span><span class="token punctuation">;</span>
            *<span class="token punctuation">)</span>
                <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$2</span>"</span> <span class="token operator">=</span> <span class="token string">"after"</span> <span class="token punctuation">]</span> <span class="token punctuation">;</span> <span class="token keyword">then</span>
                    <span class="token assign-left variable"><span class="token environment constant">PATH</span></span><span class="token operator">=</span><span class="token environment constant">$PATH</span><span class="token builtin class-name">:</span><span class="token variable">$1</span>
                <span class="token keyword">else</span>
                    <span class="token assign-left variable"><span class="token environment constant">PATH</span></span><span class="token operator">=</span><span class="token variable">$1</span><span class="token builtin class-name">:</span><span class="token environment constant">$PATH</span>
                <span class="token keyword">fi</span>
        <span class="token keyword">esac</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token environment constant">$UID</span> -gt <span class="token number">199</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable"><span class="token variable">`</span><span class="token function">id</span> -gn<span class="token variable">`</span></span>"</span> <span class="token operator">=</span> <span class="token string">"<span class="token variable"><span class="token variable">`</span><span class="token function">id</span> -un<span class="token variable">`</span></span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
       <span class="token builtin class-name">umask</span> 002
    <span class="token keyword">else</span>
       <span class="token builtin class-name">umask</span> 022
    <span class="token keyword">fi</span>
    <span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> /etc/profile.d/*.sh<span class="token punctuation">;</span> <span class="token keyword">do</span>			<span class="token comment"># 调用/etc/profile.d/*.sh</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span> -r <span class="token string">"<span class="token variable">$i</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token environment constant">$PS1</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
                <span class="token builtin class-name">.</span> <span class="token string">"<span class="token variable">$i</span>"</span>
            <span class="token keyword">else</span>
                <span class="token builtin class-name">.</span> <span class="token string">"<span class="token variable">$i</span>"</span> <span class="token operator">></span>/dev/null <span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token file-descriptor important">&amp;1</span>
            <span class="token keyword">fi</span>
        <span class="token keyword">fi</span>
    <span class="token keyword">done</span>
    <span class="token builtin class-name">unset</span> i
    <span class="token builtin class-name">unset</span> pathmunge
<span class="token keyword">fi</span></code></pre></li>
</ol></li>
<li><p>非登录的调用顺序</p>
<p>略，从<code>/etc/bashrc</code>开始</p></li>
</ul></li>
<li><p>注销时生效的环境变量配置文件</p>
<p>退出的时候只生效<code>~/.bash_logout</code>，这个文件只有在用户执行logout推出的时候生效，shutdown...不生效，文件内部默认没有内容，可以按需写一些</p></li>
</ul></li>
<li><p>Shell登录信息</p>
<ul>
<li><p><code>/etc/issue</code>在登录tty的时候显示欢迎信息，注意：<strong>是tty，只有本地登录才有效！</strong></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">CentOS release <span class="token number">6.8</span> <span class="token punctuation">(</span>Final<span class="token punctuation">)</span>
Kernel <span class="token punctuation">\</span>r on an <span class="token punctuation">\</span>m</code></pre>
<p>转义符的意义</p>
<ul>
<li><code>/l</code>： 显示第几个终端机接口</li>
<li><code>/m</code>： 显示硬件的等级（i386/i686...）</li>
<li><code>/n</code>： 显示主机的网络名称</li>
<li><code>/o</code>： 显示 domain name</li>
<li><code>/r</code>： 显示操作系统的版本</li>
<li><code>/t</code>： 显示本地端时间的时间</li>
<li><code>/s</code>： 显示操作系统的名称</li>
<li><code>/v</code>： 显示操作系统的版本</li>
</ul>
<p>一般就是加个<code>/l</code>显示tty几</p></li>
<li><p><code>/etc/issue.net</code>
用于显示远程登录的欢迎信息，<code>\d</code>,<code>\l</code>转义符不支持，这个文件只是显示的内容，至于是否显示需要显示需要修改<code>/etc/ssh/sshd_config</code>，<code>#Banner none</code>改成<code>Banner etc/issue.net</code></p></li>
<li><p><code>/etc/motd</code>不论是远程还是本地登录都会显示欢迎信息，区别是这个是在登录后的显示内容，前面的是登录前显示的</p></li>
</ul></li>
<li><p><code>stty -a</code>命令：显示所有快捷键</p>
<p><code>stty intr ^p</code>将ctrl+p快捷键作为intr</p></li>
</ul>
<h2 id="shell编程">Shell编程</h2>
<h3 id="正则表达式">正则表达式</h3>
<ul>
<li>基础正则：略，见<a
target="_blank" rel="noopener" href="http://www.liukairui.cc/%E5%B0%9A%E7%A1%85%E8%B0%B7JavaScript%E5%9F%BA%E7%A1%80%E7%89%88%E7%AC%94%E8%AE%B0/#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F">前端</a></li>
<li>正则：`grep "模式串" 匹配串/文件``
<ul>
<li><code>-v</code>取反</li>
<li><code>-E</code>扩展正则，支持更多符号，同时不需要要将<code>()</code>转义为<code>\(\)</code>了</li>
</ul></li>
</ul>
<h3 id="字符截取和替换">字符截取和替换</h3>
<ul>
<li><p><code>cut</code><strong>列提取</strong></p>
<p><code>cut [选项] 文件名</code></p>
<ul>
<li><code>-f 列号码</code>提取某一列，提取多列用,隔开即可，列编号从0开始</li>
<li><code>-d 分隔符</code>指定分割符号，默认Tab，但是一定不识别空格</li>
<li><code>-c 数字-数字</code>提取从第几个字符到第几个字符，从0计数，不写默认行首/尾</li>
</ul></li>
<li><p><code>awk</code>编程</p>
<p>这是一面文件文本处理语言，可以在Shell里面做<strong>文本提取</strong></p>
<p>awk输出支持<code>print</code>和<code>printf</code>，后者多个空格，Linux只支持<code>printf</code></p>
<ul>
<li><p><code>printf 输出类型 输出内容</code></p>
<ul>
<li><p>输出类型有：</p>
<ul>
<li><code>%s</code>字符串，<code>%ns</code><strong>至少</strong>输出n位字符串，空余的在前面不空格</li>
<li><code>%i</code>整数，<code>%ni</code><strong>至少</strong>输出n位整数，空余的在前面补空格</li>
<li><code>%f</code>浮点，<code>%n.mf</code>输出m位小数，有效位数<strong>是</strong>n(包括小数)</li>
</ul></li>
<li><p>转义符同C++</p></li>
<li><p>使用printf输出文件：<code>printf '%s' $(cat ./demo.txt)</code>但是所有的，必须手动加上控制符</p>
<p><pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># cat st</span>
<span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
<span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
<span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># printf '%s' $(cat ./st)</span>
<span class="token number">113411341134</span>
<span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># printf '%s\t%s\t%s\t%s\n' $(cat ./st)</span>
<span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
<span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
<span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span></code></pre></p>
<p>控制符可能不够，printf会循环利用，例如</p>
<p><pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># printf '%s\t%s\n' $(cat ./st)</span>
<span class="token number">1</span>       <span class="token number">1</span>
<span class="token number">3</span>       <span class="token number">4</span>
<span class="token number">1</span>       <span class="token number">1</span>
<span class="token number">3</span>       <span class="token number">4</span>
<span class="token number">1</span>       <span class="token number">1</span>
<span class="token number">3</span>       <span class="token number">4</span></code></pre></p></li>
</ul></li>
</ul></li>
<li><p>awk的基本使用</p>
<ul>
<li><p><code>awk '条件1&#123;动作1&#125; 条件2&#123;动作2&#125;...' 文件名</code></p>
<ul>
<li>条件有<code>&gt;</code>,<code>==</code>,<code>A~B</code>B是的A子串,<code>A!~B</code>B不是A的子串，<code>/正则/</code>进行正则匹配，这个被匹配对象是一
行，也就是<code>$0</code></li>
</ul></li>
<li><p>动作有printf，流程控制语句</p>
<ul>
<li>awk一次会读取文件的一行数据，将一行数据放在这样的语句中执行一次，循环执行</li>
</ul>
<p>例如</p>
<p><pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 提取34列</span>
    <span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># awk '&#123;printf $3 "\t" $4 "\n"&#125;' st</span>
    <span class="token number">3</span>       <span class="token number">4</span>
    <span class="token number">3</span>       <span class="token number">4</span>
    <span class="token number">3</span>       <span class="token number">4</span></code></pre></p></li>
<li><p><code>BEGIN</code>和<code>END</code>可以占用条件位置，后面写一些动作，告诉awk在程序执行一开始/结尾执行</p></li>
<li><p>内置变量</p>
<ul>
<li><code>$0</code>代表awk读入的整行数据</li>
<li><code>$n</code>代表读入的那一行的第n列</li>
<li><code>NF</code>当前行的列数目，注意，没有$</li>
<li><code>NR</code> 当前行是总的第几行，注意，没有$</li>
<li><code>FS</code>指定分隔符，例如<code>awk 'BEGIN&#123;FS=":"&#125;条件&#123;运行&#125;...'</code>注意，要写在BEGIN否则在第一行的时候会先读入，按照默认分隔，才设置分隔符，第一行失效</li>
</ul></li>
</ul></li>
<li><p><code>sed</code>命令</p>
<p>主要是用来<strong>修改</strong>文件中的字符串</p>
<p><code>sed [选项] '[]' 文件名</code></p>
<p>选项</p>
<ul>
<li><code>-n</code>：sed默认是将全文输出到屏幕，可以使用这个选项只输出正在处理的这一行</li>
<li><code>-i</code>：将修改的结果写入文件，默认不写入、</li>
</ul></li>
<li><p><code>-e</code>：执行多个动作，例如<code>sed -ie '2d;3d' ./st</code></p>
<ul>
<li><code>-r</code>：支持扩展正则</li>
</ul>
<p>动作，注意加''</p>
<ul>
<li><p><code>np</code>：打印第n行</p></li>
<li><p><code>a,bd</code>：删除a行到b行，只删除一行就直接写数字了</p></li>
<li><p><code>na 内容\</code>：在n行后新建一行追加内容，除了最后一行都要写<code>\</code></p></li>
<li><p><code>ni 内容\</code>：在n行前新建一行追加内容，除了最后一行都要写<code>\</code></p></li>
</ul>
<p><pre class="language-bash" data-language="bash"><code class="language-bash">  <span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># sed '1a lalala' ./st</span>
  nh      jk      jk      l
  lalala
  <span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
  <span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
  <span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># sed '1a la\la\la' ./st</span>
  nh      jk      jk      l
  lalala
  <span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
  <span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
  <span class="token punctuation">[</span>root@10 ~<span class="token punctuation">]</span><span class="token comment"># sed '1a la\</span>
  <span class="token operator">></span> la<span class="token punctuation">\</span>
  <span class="token operator">></span> la' ./st
  nh      jk      jk      l
  la
  la
  la
  <span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span>
<span class="token number">1</span>       <span class="token number">1</span>       <span class="token number">3</span>       <span class="token number">4</span></code></pre></p>
<p>注意，每一行都要<code>\</code></p>
<ul>
<li><p><code>a,bc 内容\</code>将a-b行替换为内容</p></li>
<li><p><code>[行范围] s/旧内容/新内容/g</code>文本替换，不写范围就是全文</p></li>
</ul></li>
</ul>
<h3 id="字符处理命令">字符处理命令</h3>
<ul>
<li><p><code>sort</code>命令</p>
<p><code>sort 文件名</code>按照文件开头第一个字母排序，排序方法与C++字符串比大小一样</p>
<ul>
<li><code>-r</code>：逆序</li>
<li><code>-n</code>：按照数字而非字符串排序</li>
<li><code>-t ":" -k 3,5</code>设置:为分隔符，参与排序的内容是第三列的第一个到第五列的最后一个</li>
<li><code>-u</code>：删除重复行</li>
</ul></li>
<li><p><code>uniq</code> 命令</p>
<p><code>uniq 文件名</code>删除重复行</p>
<p><code>uniq -i 文件名</code>忽略大小写</p></li>
<li><p><code>wc</code>命令</p>
<p><code>wc 文件名</code>字数统计，得到的结果分别是有多少行，多少个单词，多少个字符</p>
<ul>
<li><code>-l</code>：只显示有多少行</li>
<li><code>-w</code>：只显示有多少单词</li>
<li><code>-m</code>：只显示有多少字符</li>
</ul></li>
</ul>
<h3 id="条件判断">条件判断</h3>
<ul>
<li><p>判断文件类型</p>
<ul>
<li><code>-b 文件</code>：判断文件是否存在&amp;&amp;文件是不是块设备文件</li>
<li><code>-c 文件</code>：判断文件是否存在&amp;&amp;文件是不是字符设备文件</li>
<li><code>-d 文件</code>：判断文件是否存在&amp;&amp;文件是不是目录文件</li>
<li><code>-e 文件</code>：判断文件是否存在</li>
<li><code>-f 文件</code>：判断文件是否存在&amp;&amp;文件是不是普通文件</li>
<li><code>-L 文件</code>：判断文件是否存在&amp;&amp;文件是不是符号链接文件</li>
<li><code>-p 文件</code>：判断文件是否存在&amp;&amp;文件是不是管道文件</li>
<li><code>-s 文件</code>：判断文件是否存在&amp;&amp;文件是不是空文件</li>
<li><code>-S 文件</code>：判断文件是否存在&amp;&amp;文件是不是套接字文件</li>
</ul>
<p>使用的方法有几种</p>
<ul>
<li><code>test -X 文件</code>然后<code>echo $?</code>，两个命令不能写在一起</li>
<li><code>[ -X 文件 ]</code>然后<code>echo $?</code>，两个命令不能写在一起</li>
<li><code>[ -b ./st ] &amp;&amp; echo "T" || echo "F"</code></li>
</ul></li>
<li><p>判断文件权限</p>
<ul>
<li><code>-r</code>：文件有读权限</li>
<li><code>-w</code>：文件有写权限</li>
<li><code>-x</code>：文件有执行权限</li>
<li><code>-u</code>：文件有SUID权限</li>
<li><code>-g</code>：文件有SGID权限</li>
<li><code>-k</code>：文件有SBIT权限</li>
</ul>
<p>这个不能区分是所属组还是所有者，只要有人有这个权限就是true</p></li>
<li><p>文件比较</p>
<ul>
<li><code>文件1 -ot 文件2</code>文件1是不是比文件2老</li>
<li><code>文件1 -nt 文件2</code>文件1是不是比文件2新</li>
<li><code>文件1 -ef 文件2</code>文件1是不是与文件2同inode(是不是硬链接)</li>
</ul></li>
<li><p>两个整数之间比较</p>
<ul>
<li><code>a -eq b</code>：判断a和b是否相等，与<code>==</code>不同，<code>==</code>是字符串比较</li>
<li><code>a -ne b</code>：判断a和b是否不相等</li>
<li><code>a -gt b</code>：判断a是否大于b</li>
<li><code>a -lt b</code>：判断a是否小于b</li>
<li><code>a -ge b</code>：判断a是否大于等于b</li>
<li><code>a -le b</code>：判断a是否小于等于b</li>
</ul></li>
<li><p>字符串判断</p>
<ul>
<li><code>-z 字符串</code>：判断字符串是否为空</li>
<li><code>-n 字符串</code>：判断字符串是否非空</li>
<li><code>字符串1 == 字符串2</code>：判断字符串相等</li>
<li><code>字符串1 != 字符串2</code>：判断字符串不等​</li>
</ul></li>
<li><p>逻辑与或非</p>
<ul>
<li><code>判断1 -a 判断2</code>：与</li>
<li><code>判断1 -o 判断2</code>：或</li>
<li><code>!判断</code>：非</li>
</ul></li>
</ul>
<h3 id="流程控制语句">流程控制语句</h3>
<ul>
<li><p><code>if</code>语句</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">if</span> <span class="token punctuation">[</span> 条件判断 <span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">then</span>
	程序
<span class="token keyword">fi</span></code></pre>
<p>语法比较刁钻，条件判断和<code>[]</code>中间一定要有空格，如果then和判断式子同行，需要中间写一个<code>;</code>然后以<code>fi</code>结尾，也可以写成</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">if</span> <span class="token punctuation">[</span> 条件判断式 <span class="token punctuation">]</span>
	<span class="token keyword">then</span>
		程序
<span class="token keyword">fi</span></code></pre>
<p>也有双分支</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">if</span> <span class="token punctuation">[</span> 条件判断 <span class="token punctuation">]</span>
	<span class="token keyword">then</span>
		程序
	<span class="token keyword">else</span>
		程序
<span class="token keyword">fi</span></code></pre></li>
<li><p><code>case</code>语句</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">case</span> $变量 <span class="token keyword">in</span>
	<span class="token string">"值1"</span><span class="token punctuation">)</span>
		程序
		<span class="token punctuation">;</span><span class="token punctuation">;</span>
	<span class="token string">"值2"</span><span class="token punctuation">)</span>
		程序
		<span class="token punctuation">;</span><span class="token punctuation">;</span>
	*<span class="token punctuation">)</span>
		如果都没有匹配就执行程序
		<span class="token punctuation">;</span><span class="token punctuation">;</span>
<span class="token keyword">esac</span></code></pre></li>
<li><p><code>for</code>循环</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">for</span> 变量 <span class="token keyword">in</span> 值1 值2 值3<span class="token punctuation">..</span>.
	<span class="token keyword">do</span>
		程序
	<span class="token keyword">done</span></code></pre>
<p>还有传统的写法</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">for</span> <span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>$n<span class="token punctuation">;</span>i<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">))</span></span>	<span class="token comment">#先进的shell已经支持i++了</span>
	<span class="token keyword">do</span>
		程序
	<span class="token keyword">done</span></code></pre>
<p>注意这里for里面的i全部没有$，看起来这第二种方法似乎更好用，但是针对实际运维的场景来看，第一个更方便，
原因是运维的for一般是用来处理一系列内容，数目可能是不确定的，但是内容可以直接拿到，于是用第一种方法可以方便的进行迭代</p>
<p>批量解压缩</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token builtin class-name">cd</span> /root/sh
<span class="token builtin class-name">echo</span> <span class="token string">""</span> <span class="token operator">>></span> log.txt
<span class="token function">ls</span> -a *.zip <span class="token operator">>></span> log.txt <span class="token operator"><span class="token file-descriptor important">2</span>>></span> /dev/null
<span class="token keyword">for</span> <span class="token for-or-select variable">filename</span> <span class="token keyword">in</span> <span class="token variable"><span class="token variable">$(</span><span class="token function">cat</span> log.txt<span class="token variable">)</span></span>
        <span class="token keyword">do</span>
                <span class="token function">unzip</span> <span class="token variable">$filename</span> <span class="token operator">&amp;></span> /dev/null
        <span class="token keyword">done</span>
<span class="token builtin class-name">echo</span> END
<span class="token function">rm</span> -rf log.txt</code></pre>
<p>批量添加用户</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token builtin class-name">read</span> -p <span class="token string">"input username"</span> -t <span class="token number">30</span> name
<span class="token builtin class-name">read</span> -p <span class="token string">"input PSW"</span> -t <span class="token number">30</span> psw
<span class="token builtin class-name">read</span> -p <span class="token string">"input num"</span> -t <span class="token number">30</span> num
<span class="token keyword">if</span> <span class="token punctuation">[</span> -z name -o -z psw -o -z num -o -z <span class="token variable"><span class="token variable">$(</span> <span class="token builtin class-name">echo</span> num <span class="token operator">|</span> <span class="token function">sed</span> <span class="token string">'s/[0-9]//g'</span> <span class="token variable">)</span></span> <span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">then</span>
        <span class="token builtin class-name">exit</span> <span class="token number">1</span>
<span class="token keyword">fi</span>
<span class="token keyword">for</span> <span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>$num<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">))</span></span>
        <span class="token keyword">do</span>
                adduser <span class="token variable">$name</span><span class="token variable">$i</span> <span class="token operator">&amp;></span> /dev/null
                <span class="token builtin class-name">echo</span> <span class="token variable">$psw</span> <span class="token operator">|</span> <span class="token function">passwd</span> --stdin <span class="token variable">$name</span><span class="token variable">$i</span> <span class="token operator">&amp;></span> /dev/null
                chage -d <span class="token number">0</span> <span class="token variable">$name</span><span class="token variable">$i</span> <span class="token operator">&amp;></span> /dev/null
        <span class="token keyword">done</span>
<span class="token builtin class-name">echo</span> END</code></pre>
<p>删除所有普通用户</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token assign-left variable">users</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token function">cat</span> /etc/passwd <span class="token operator">|</span> <span class="token function">grep</span> /bin/bash <span class="token operator">|</span> <span class="token function">grep</span> -v root<span class="token variable">)</span></span>
<span class="token keyword">for</span> <span class="token for-or-select variable">user</span> <span class="token keyword">in</span> <span class="token variable">$users</span>
        <span class="token keyword">do</span>
                <span class="token assign-left variable">username</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $user <span class="token operator">|</span> <span class="token function">cut</span> -d <span class="token string">":"</span> -f <span class="token number">1</span><span class="token variable">)</span></span>
                <span class="token function">userdel</span> <span class="token variable">$username</span>
                <span class="token function">rm</span> -rf /home/<span class="token variable">$username</span>
                <span class="token function">rm</span> -rf /var/spool/mail/<span class="token variable">$username</span>
                <span class="token builtin class-name">echo</span> <span class="token string">"DEL <span class="token variable">$username</span>"</span>
        <span class="token keyword">done</span>
<span class="token builtin class-name">echo</span> END</code></pre></li>
<li><p><code>while</code>循环</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">while</span> <span class="token punctuation">[</span> 条件 <span class="token punctuation">]</span>
	<span class="token keyword">do</span>
		程序
	<span class="token keyword">done</span></code></pre></li>
<li><p><code>until</code>循环</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">until</span> <span class="token punctuation">[</span> 条件 <span class="token punctuation">]</span>
	<span class="token keyword">do</span>
		程序
	<span class="token keyword">done</span></code></pre>
<p>while是条件真则循环，until是条件假循环</p></li>
</ul>
<h3 id="特殊流程控制语句">特殊流程控制语句</h3>
<ul>
<li><p><code>exit</code>语句</p>
<p>系统有exit指令，可以用来退出当前用户的登录状态，Bash也有exit语句，可以在后面加返回值用来退出程序，后面需要加返回值，可以使用<code>$?</code>查询，如果不加返回值，那么会赋予上次返回的返回值</p></li>
<li><p><code>continue</code>与<code>break</code>语句</p>
<p>同C</p></li>
</ul>
<h2 id="启动引导与修复">启动引导与修复</h2>
<h3 id="系统的运行级别">系统的运行级别</h3>
<ul>
<li><p>运行级别有7个</p>
<table>
<thead>
<tr class="header">
<th>运行级别</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>0</td>
<td>关机</td>
</tr>
<tr class="even">
<td>1</td>
<td>单用户模式，类似于windows的安全模式，用于修复系统</td>
</tr>
<tr class="odd">
<td>2</td>
<td>不完全的命令行模式，不包含NFS服务(Linux间文件传输服务)</td>
</tr>
<tr class="even">
<td>3</td>
<td>完全命令模式</td>
</tr>
<tr class="odd">
<td>4</td>
<td>系统保留</td>
</tr>
<tr class="even">
<td>5</td>
<td>图像模式</td>
</tr>
<tr class="odd">
<td>6</td>
<td>重启</td>
</tr>
</tbody>
</table></li>
<li><p>查看运行级别可以使用命令<code>runlevel</code>，有两个结果，第一个是在进入这个级别之前上一个级别是多少，没有是N，第二个值是当前运行级别</p></li>
<li><p>使用<code>init N</code>进入N级，不得用于开机或者重启，容易丢数据</p></li>
<li><p>可以修改<code>/etc/inittab</code>修改开机后的默认级别，修改<code>id:3:initdefault:</code>中间的数字即可</p></li>
<li><p><code>/etc/rc.d/rc.local</code>中可以写入希望开机后登录前自动执行的命令，也可以修改他的软链接<code>/etc/rc.local</code></p></li>
</ul>
<h3 id="启动引导程序">启动引导程序</h3>
<p>早期的启动引导程序是Lilo，现在基本上是Grub，优势有</p>
<ul>
<li>支持多文件系统</li>
<li>可以在主程序中查找内核文件</li>
<li>可以交互修改启动选项</li>
<li>可以动态修改配置</li>
</ul>
<p>Grub就存放在<code>/boot/</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@bogon boot<span class="token punctuation">]</span><span class="token comment"># ll</span>
总用量 <span class="token number">31737</span>
-rw-r--r--. <span class="token number">1</span> root root   <span class="token number">108103</span> <span class="token number">5</span>月  <span class="token number">11</span> <span class="token number">2016</span> config-2.6.32-642.el6.x86_64
drwxr-xr-x. <span class="token number">3</span> root root     <span class="token number">1024</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:28 efi
drwxr-xr-x. <span class="token number">2</span> root root     <span class="token number">1024</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 grub
-rw-------. <span class="token number">1</span> root root <span class="token number">25270392</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 initramfs-2.6.32-642.el6.x86_64.img
drwx------. <span class="token number">2</span> root root    <span class="token number">12288</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:23 lost+found
-rw-r--r--. <span class="token number">1</span> root root   <span class="token number">215559</span> <span class="token number">5</span>月  <span class="token number">11</span> <span class="token number">2016</span> symvers-2.6.32-642.el6.x86_64.gz
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">2615003</span> <span class="token number">5</span>月  <span class="token number">11</span> <span class="token number">2016</span> System.map-2.6.32-642.el6.x86_64
-rwxr-xr-x. <span class="token number">1</span> root root  <span class="token number">4264528</span> <span class="token number">5</span>月  <span class="token number">11</span> <span class="token number">2016</span> vmlinuz-2.6.32-642.el6.x86_64</code></pre>
<p><code>symvers-2.6.32-642.el6.x86_64.gz</code>是编译过的二进制内核，源码在<code>/usr/src/kernels</code>，进入Grub看到</p>
<pre class="language-shell" data-language="shell"><code class="language-shell"><span class="token punctuation">[</span>root@bogon grub<span class="token punctuation">]</span><span class="token comment"># ll</span>
总用量 <span class="token number">274</span>
-rw-r--r--. <span class="token number">1</span> root root     <span class="token number">63</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 device.map
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">13428</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 e2fs_stage1_5
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">12636</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 fat_stage1_5
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">11780</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 ffs_stage1_5
-rw-------. <span class="token number">1</span> root root    <span class="token number">745</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 grub.conf
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">11772</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 iso9660_stage1_5
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">13284</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 jfs_stage1_5
lrwxrwxrwx. <span class="token number">1</span> root root     <span class="token number">11</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 menu.lst -<span class="token operator">></span> ./grub.conf
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">11972</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 minix_stage1_5
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">14428</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 reiserfs_stage1_5
-rw-r--r--. <span class="token number">1</span> root root   <span class="token number">1341</span> <span class="token number">11</span>月 <span class="token number">15</span> <span class="token number">2010</span> splash.xpm.gz
-rw-r--r--. <span class="token number">1</span> root root    <span class="token number">512</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 stage1
-rw-r--r--. <span class="token number">1</span> root root <span class="token number">126148</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 stage2
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">12040</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 ufs2_stage1_5
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">11380</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 vstafs_stage1_5
-rw-r--r--. <span class="token number">1</span> root root  <span class="token number">13980</span> <span class="token number">3</span>月   <span class="token number">1</span> <span class="token number">13</span>:30 xfs_stage1_5</code></pre>
<p>主要文件是<code>grub.conf</code>,<code>menu.lst</code>,<code>splash.xpm.gz</code>，第一个是配置文件，第二个是第一个的软连接，第三个是Grub启动的背景图片，格式略微奇怪</p>
<ul>
<li><p>Grub配置文件</p>
<ul>
<li><p>分区表示法：</p>
<p><code>hd(X,Y)</code>，X表示是第几块硬盘(从0开始)，Y表示是第几个分区(从0开始)，例如sdb1就是<code>hd(1,0)</code></p></li>
<li><p>Grub配置文件</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token comment">#boot=/dev/sda</span>
<span class="token assign-left variable">default</span><span class="token operator">=</span><span class="token number">0</span>
<span class="token assign-left variable">timeout</span><span class="token operator">=</span><span class="token number">5</span>
<span class="token assign-left variable">splashimage</span><span class="token operator">=</span><span class="token punctuation">(</span>hd0,0<span class="token punctuation">)</span>/grub/splash.xpm.gz
hiddenmenu
<span class="token comment">################################################################</span>
title CentOS <span class="token number">6</span> <span class="token punctuation">(</span><span class="token number">2.6</span>.32-642.el6.x86_64<span class="token punctuation">)</span>
        root <span class="token punctuation">(</span>hd0,0<span class="token punctuation">)</span>
        kernel /vmlinuz-2.6.32-642.el6.x86_64 ro <span class="token assign-left variable">root</span><span class="token operator">=</span>UUID<span class="token operator">=</span>654883f4-8efe-4c7f-b60b-7886a3bf8309 rd_NO_LUKS  <span class="token assign-left variable">KEYBOARDTYPE</span><span class="token operator">=</span>pc <span class="token assign-left variable">KEYTABLE</span><span class="token operator">=</span>us rd_NO_MD <span class="token assign-left variable">crashkernel</span><span class="token operator">=</span>auto <span class="token assign-left variable"><span class="token environment constant">LANG</span></span><span class="token operator">=</span>zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-642.el6.x86_64.img</code></pre>
<p>第一部分是整体配置，分别是默认启动系统，等待时间(s，-1表示一直等待)，背景图像，隐藏菜单界面，只显示Xs后加载CentOS</p>
<p>第二部分是系统的配置，分别是</p>
<ul>
<li><p>root位置</p></li>
<li><p>内核</p>
<ul>
<li>内核位置</li>
<li>ro：<strong>启动</strong>的时候只读挂载root</li>
<li>root盘的UUID</li>
<li>rd_NO_LUKS：禁用磁盘加密</li>
<li>KEYBOARDTYPE=pc KEYTABLE=us</li>
<li>rd_NO_MD：禁用软RAID</li>
<li>crashkernel=auto：自动为crashkernel留内存</li>
<li>LANG=zh_CN.UTF-8：语言环境</li>
<li>rd_NO_LVM：禁用LVM</li>
<li>rd_NO_DM：禁用硬RAID</li>
<li>rhgb：用图片代替启动过程的文字信息，启动后使用dmesg查看文字信息</li>
<li>quiet：隐藏启动信息，只显示重要信息</li>
</ul>
<p>注意，这里的禁用RAID，LVM指的是启动的时候禁用，开机后正常使用</p></li>
<li><p>虚拟文件系统</p></li>
</ul></li>
<li><p>Grub加密</p>
<p>忘记root密码后可以在Grub中按e找到密码，但是这样看起来有点不安全，可以加密一下Grub也就是在进入Grub高级菜单的时候要输入密码</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@bogon grub<span class="token punctuation">]</span><span class="token comment"># grub-md5-crypt		# 使用加密命令得到密码的MD5码</span>
Password:
Retype password:
<span class="token variable">$1</span><span class="token variable">$3SGaL1</span><span class="token variable">$xbpqvWCli69UbOFIudJ9Y0</span>
<span class="token punctuation">[</span>root@bogon grub<span class="token punctuation">]</span><span class="token comment"># vim grub.conf		# 进入Grub配置文件</span>
<span class="token assign-left variable">timeout</span><span class="token operator">=</span><span class="token number">5</span>
password --md5 <span class="token variable">$1</span><span class="token variable">$3SGaL1</span><span class="token variable">$xbpqvWCli69UbOFIudJ9Y0</span>		<span class="token comment"># 写下password --md5 然后粘贴md5码，必须写在timeout与splashimage中间，其他位置无效</span>
<span class="token assign-left variable">splashimage</span><span class="token operator">=</span><span class="token punctuation">(</span>hd0,0<span class="token punctuation">)</span>/grub/splash.xpm.gz</code></pre>
<p><strong>注意：必须写在timeout与splashimage中间，其他位置无效</strong>，这个密码只是进入编辑模式才要密码，如果想要开机业需要密码可以在配置文件中</p>
<pre class="language-diff" data-language="diff"><code class="language-diff">title CentOS 6 (2.6.32-642.el6.x86_64)
<span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line">		lock
</span></span><span class="token unchanged"><span class="token prefix unchanged"> </span><span class="token line">       root (hd0,0)</span></span></code></pre>
<p>但是不要犯二，这样就没法远程了</p></li>
</ul></li>
</ul>
<h3 id="系统修复模式">系统修复模式</h3>
<p>有两种修复模式，单用户和光盘修复，单用户可以做密码找回，还原配置文件这样的简单操作，光盘做的比较多</p>
<p><strong>所有的修复模式都是需要本机的，千万不要依赖这玩意</strong></p>
<ul>
<li><p>单用户模式</p>
<p>进入Grub菜单，选择输入e，选择内核那一条，输入e，在最后加入<code>1</code>空格和1就可以临时修改Grub，按下b可以启动到单用户模式，之后，我们可以不使用用户密码直接登录</p>
<p>修改root密码</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">passwd</span> root</code></pre>
<p>就完事</p></li>
<li><p>光盘修复模式</p>
<p>修改BIOS顺序进入光盘，选第三个<code>rescue installed system</code>模式，选择语言，选英文不要选中文，us美式键盘，选择是否启动网络，弹出警告界面continue，一路OK，选择shell模式，进入shell，此时Shell会显示在/目录，但是这个不是硬盘的根目录而是光盘的，因为你在光盘的系统上，计算机上的硬盘被作为数据盘挂载到了<code>/mnt/sysimage</code>，需要chroot将硬盘根目录作为根目录<code>chroot /mnt/sysimage</code>，之后直接修改就可以了</p></li>
</ul>
<h2 id="服务管理">服务管理</h2>
<h3 id="服务的简介与分类">服务的简介与分类</h3>
<p>服务的分类</p>
<ul>
<li>RPM包安装的服务
<ul>
<li>独立的服务：直接放在内存里面的，可以直接访问</li>
<li>基于xinetd的服务：将xinetd放在内存里面，将服务放在ximetd后面，节约内存，但是慢，无法直接访问</li>
</ul></li>
<li>源码包安装的服务</li>
</ul>
<p>区分是独立服务还是xinetd服务的方法是<code>chkconfig --list</code>，其中，显示的0-6就是在0-6运行级别下是否开启，这些都是独立服务，xinetd服务会在最后单独列出</p>
<h3 id="rpm包的启动和自启动">RPM包的启动和自启动</h3>
<ul>
<li><p>独立服务</p>
<ul>
<li><p>启动</p>
<p><code>绝对路径 start</code>例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">/etc/init.d/httpd start</code></pre>
<p><code>service 服务名 start|stop|restart|...</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">service</span> httpd start</code></pre></li>
<li><p>自启动</p>
<ul>
<li><p><code>chkconfig</code>方法</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">checkconfig  <span class="token punctuation">[</span>--level 运行级别<span class="token punctuation">]</span> <span class="token punctuation">[</span>独立服务名<span class="token punctuation">]</span> <span class="token punctuation">[</span>on<span class="token operator">|</span>off<span class="token punctuation">]</span></code></pre>
<p>例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">chkconfig</span> --level <span class="token number">2345</span> httpd on</code></pre>
<p>虽然4是未分配，但是就是习惯这么写，这个设置的是自启动，所以下次启动生效</p></li>
<li><p>修改<code>/etc/rc.d/rc.local</code>文件，直接写<code>绝对路径 start</code>此处不要写<code>service 服务名 start</code>可能数据库还没开</p></li>
<li><p><code>ntsysv</code>图形化配置，这个开启的只开启了3级别(命令行界面)，开启后chechconfig可查，但是推荐<code>rc.local</code>方法</p></li>
</ul></li>
</ul></li>
<li><p>基于xinetd服务</p>
<ul>
<li><p>启动</p>
<ul>
<li><code>vim /etc/xinetd.d/服务名</code>，修改为<code>disable:no</code>，然后重启xinetd<code>service xinetd restart</code></li>
</ul></li>
<li><p>自启动</p>
<p>xinetd服务无法自启动，需要将xinetd作为自启动，里面的所有<code>disable:no</code>是服务就可以自启动，这个是极其不合理的，xinetd也基本被淘汰了</p></li>
</ul></li>
<li><p>源码包的服务</p>
<ul>
<li><p>启动：使用的是源码包提供的启动脚本+选项进行管理</p></li>
<li><p>自启动： 修改<code>/etc/rc.d/rc.local</code>文件</p></li>
<li><p>让源码包被service识别</p>
<p>不推荐这么做!!!</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">ln</span> -s 源码启动脚本 /etc/init.d/服务名</code></pre>
<p>例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">ln</span> -s /usr/local/apache2/bin/apachectl /etc/init.d/apache
<span class="token function">service</span> apache restart</code></pre></li>
<li><p>让源码包被chkconfig识别，在/etc/init.d/服务名里加上</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># chkconfig: 运行级别 启动顺序 关闭顺序</span>
<span class="token comment"># description: 随便写点描述</span></code></pre></li>
</ul></li>
</ul>
<h3 id="linux中常见的服务的作用">Linux中常见的服务的作用</h3>
<table>
<colgroup>
<col style="width: 18%" />
<col style="width: 75%" />
<col style="width: 5%" />
</colgroup>
<thead>
<tr class="header">
<th>服务名称</th>
<th>功能简介</th>
<th>建议</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>acpid</td>
<td>电源管理接口。如果是笔记本电脑用户，则建议开启，可以监听内核层的相关电源事件</td>
<td>开启</td>
</tr>
<tr class="even">
<td>anacron</td>
<td>系统的定时任务程序。是 cron
的一个子系统，如果定时任务错过了执行时间，则可以通过 anacron
继续唤醒执行</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>alsasound</td>
<td>alsa 声卡驱动。如果使用 alsa 声卡，则开启</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>apmd</td>
<td>电源管理模块。如果支持 acpid，就不需要 apmd，可以关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>atd</td>
<td>指定系统在特定时间执行某个任务，只能执行一次。如果需要则开启，但我们一般使用
crond 来执行循环定时任务</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>auditd</td>
<td>审核子系统。如果开启了此服务，那么 SELinux 的审核信息会写入
/var/log/audit/ audit.log 文件；如果不开启，那么审核信息会记录在 syslog
中</td>
<td>开启</td>
</tr>
<tr class="odd">
<td>autofs</td>
<td>让服务器可以自动挂载网络中其他服务器的共享数据,一般用来自动挂载 NFS
服务。如果没有 NFS 服务，则建议关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>avahi-daemon</td>
<td>avahi 是 zeroconf 协议的实现，它可以在没有 DNS
服务的局域网里发现基于 zeroconf 协议的设备和服务。除非有兼容设备或使用
zeroconf 协议，否则关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>bluetooth</td>
<td>蓝牙设备支持。一般不会在服务器上启用蓝牙设备，关闭它</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>capi</td>
<td>仅对使用 ISND 设备的用户有用</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>chargen-dgram</td>
<td>使用 UDP 协议的 chargen server。其主要提供类似远程打字的功能</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>chargen-stream</td>
<td>同上</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>cpuspeed</td>
<td>可以用来调整 CPU 的频率。当闲置时，可以自动降低 CPU
频率来节省电量</td>
<td>开启</td>
</tr>
<tr class="even">
<td>crond</td>
<td>系统的定时任务，一般的 Linux
服务器都需要定时任务来协助系统维护。建议开启</td>
<td>开启</td>
</tr>
<tr class="odd">
<td>cvs</td>
<td>一个版本控制系统</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>daytime-dgram</td>
<td>使用 TCP 协议的 daytime
守护进程，该协议为客户机实现从远程服务器获取日期和时间的功能</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>daytime-slream</td>
<td>同上</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>dovecot</td>
<td>邮件服务中 POP3/IMAP
服务的守护进程，主要用来接收信件。如果启动了邮件服务则开启：否则关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>echo-dgram</td>
<td>服务器回显客户服务的进程</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>echo-stream</td>
<td>同上</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>firstboot</td>
<td>系统安装完成后，有一个欢迎界面，需要对系统进行初始设定，这就是这个服务的作用。既然不是第一次启动了，则建议关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>gpm</td>
<td>在字符终端 (ttyl~tty6)
中可以使用鼠标复制和粘贴，这就是这个服务的功能</td>
<td>开启</td>
</tr>
<tr class="odd">
<td>haldaemon</td>
<td>检测和支持 USB 设备。如果是服务器则可以关闭，个人机则建议开启</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>hidd</td>
<td>蓝牙鼠标、键盘等蓝牙设备检测。必须启动 bluetooth 服务</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>hplip</td>
<td>HP 打印机支持，如果没有 HP 打印机则关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>httpd</td>
<td>apache 服务的守护进程。如果需要启动 apache，就开启</td>
<td>开启</td>
</tr>
<tr class="odd">
<td>ip6tables</td>
<td>IPv6 的防火墙。目前 IPv6 协议并没有使用，可以关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>iptables</td>
<td>防火墙功能。Linux
中的防火墙是内核支持功能。这是服务器的主要防护手段，必须开启</td>
<td>开启</td>
</tr>
<tr class="odd">
<td></td>
<td>IrDA
提供红外线设备（笔记本电脑、PDA’s、手机、计算器等）间的通信支持。建议关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>irqbalance</td>
<td>支持多核处理器，让 CPU
可以自动分配系统中断（IRQ)，提高系统性能。目前服务器多是多核
CPU，请开启</td>
<td>开启</td>
</tr>
<tr class="odd">
<td>isdn</td>
<td>使用 ISDN 设备连接网络。目前主流的联网方式是光纤接入和 ADSL，ISDN
己经非常少见，请关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>kudzu</td>
<td>该服务可以在开机时进行硬件检测，并会调用相关的设置软件。建议关闭，仅在需要时开启</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>lvm2-monitor</td>
<td>该服务可以让系统支持LVM逻辑卷组，如果分区采用的是LVM方式，那么应该开启。建议开启</td>
<td>开启</td>
</tr>
<tr class="even">
<td>mcstrans</td>
<td>SELinux 的支持服务。建议开启</td>
<td>开启</td>
</tr>
<tr class="odd">
<td>mdmonitor</td>
<td>该服务用来监测 Software RAID 或 LVM
的信息。不是必需服务，建议关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>mdmpd</td>
<td>该服务用来监测 Multi-Path 设备。不是必需服务，建议关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>messagebus</td>
<td>这是 Linux 的 IPC (Interprocess
Communication，进程间通信）服务，用来在各个软件中交换信息。建议关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>microcode _ctl</td>
<td>Intel 系列的 CPU 可以通过这个服务支持额外的微指令集。建议关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>mysqld</td>
<td>MySQL 数据库服务器。如果需要就开启；否则关闭</td>
<td>开启</td>
</tr>
<tr class="even">
<td>named</td>
<td>DNS 服务的守护进程，用来进行域名解析。如果是 DNS
服务器则开启；否则关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>netfs</td>
<td>该服务用于在系统启动时自动挂载网络中的共享文件空间，比如 NFS、Samba
等。 需要就开启，否则关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>network</td>
<td>提供网络设罝功能。通过这个服务来管理网络，建议开启</td>
<td>开启</td>
</tr>
<tr class="odd">
<td>nfs</td>
<td>NFS (Network File System) 服务，Linux 与 Linux
之间的文件共享服务。需要就开启，否则关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>nfslock</td>
<td>在 Linux 中如果使用了 NFS
服务，那么，为了避免同一个文件被不同的用户同时编辑，所以有这个锁服务。有
NFS 时开启，否则关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>ntpd</td>
<td>该服务可以通过互联网自动更新系统时间.使系统时间永远准确。需要则开启，但不是必需服务</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>pcscd</td>
<td>智能卡检测服务，可以关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>portmap</td>
<td>用在远程过程调用 (RPC) 的服务，如果没有任何 RPC
服务，则可以关闭。主要是 NFS 和 NIS 服务需要</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>psacct</td>
<td>该守护进程支持几个监控进程活动的工具</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>rdisc</td>
<td>客户端 ICMP 路由协议</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>readahead_early</td>
<td>在系统开启的时候，先将某些进程加载入内存整理，可以加快启动速度</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>readahead_later</td>
<td>同上</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>restorecond</td>
<td>用于给 SELinux 监测和重新加载正确的文件上下文。如果开启
SELinux，则需要开启</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>rpcgssd</td>
<td>与 NFS 有关的客户端功能。如果没有 NFS 就关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>rpcidmapd</td>
<td>同上</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>rsync</td>
<td>远程数据备份守护进程</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>sendmail</td>
<td>sendmail 邮件服务的守护进程。如果有邮件服务就开启；否则关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>setroubleshoot</td>
<td>该服务用于将 SELinux 相关信息记录在日志 /var/log/messages
中。建议开启</td>
<td>开启</td>
</tr>
<tr class="even">
<td>smartd</td>
<td>该服务用于自动检测硬盘状态。建议开启</td>
<td>开启</td>
</tr>
<tr class="odd">
<td>smb</td>
<td>网络服务 samba 的守护进程。可以让 Linux 和 Windows
之间共享数据。如果需要则开启</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>squid</td>
<td>代理服务的守护进程。如果需要则开启：否则关闭</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>sshd</td>
<td>ssh
加密远程登录管理的服务。服务器的远程管理必须使用此服务，不要关闭</td>
<td>开启</td>
</tr>
<tr class="even">
<td>syslog</td>
<td>日志的守护进程</td>
<td>开启</td>
</tr>
<tr class="odd">
<td>vsftpd</td>
<td>vsftp 服务的守护进程。如果需要 FTP 服务则开启；否则关闭</td>
<td>关闭</td>
</tr>
<tr class="even">
<td>xfs</td>
<td>这是 X Window
的字体守护进程，为图形界面提供字体服务。如果不启动图形界面，就不用开启</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>xinetd</td>
<td>超级守护进程。如果有依赖 xinetd 的服务，就必须开启</td>
<td>开启</td>
</tr>
<tr class="even">
<td>ypbind</td>
<td>为 NIS (网络信息系统）客户机激活 ypbind 服务进程</td>
<td>关闭</td>
</tr>
<tr class="odd">
<td>yum-updatesd</td>
<td>yum 的在线升级服务</td>
<td>关闭</td>
</tr>
</tbody>
</table>
<h2 id="系统管理">系统管理</h2>
<h3 id="进程管理">进程管理</h3>
<ul>
<li><p>作用</p>
<ul>
<li>帮助工程师监控服务器状态而不是简单的结束进程，一般判断服务器健康状态有70-90原则，也就是内存占用不超过70%,CPU占用不超过90%</li>
<li>查看系统中所有的进程，查看系统中正在运行的服务</li>
<li>杀死进程，这是最不常用的作用</li>
</ul></li>
<li><p>查看进程命令</p>
<ul>
<li><p><code>ps aux</code>查询系统中所有正在运行的进程，注意没有-</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@bogon ~<span class="token punctuation">]</span><span class="token comment"># ps aux</span>
<span class="token environment constant">USER</span>       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         <span class="token number">1</span>  <span class="token number">0.0</span>  <span class="token number">0.1</span>  <span class="token number">19352</span>  <span class="token number">1544</span> ?        Ss   00:08   <span class="token number">0</span>:02 /sbin/init
root         <span class="token number">2</span>  <span class="token number">0.0</span>  <span class="token number">0.0</span>      <span class="token number">0</span>     <span class="token number">0</span> ?        S    00:08   <span class="token number">0</span>:00 <span class="token punctuation">[</span>kthreadd<span class="token punctuation">]</span>
root         <span class="token number">3</span>  <span class="token number">0.0</span>  <span class="token number">0.0</span>      <span class="token number">0</span>     <span class="token number">0</span> ?        S    00:08   <span class="token number">0</span>:00 <span class="token punctuation">[</span>migration/0<span class="token punctuation">]</span></code></pre>
<p>每列的意思分别是</p>
<ul>
<li>USER：产生的用户</li>
<li>PID：进程ID</li>
<li>%CPU：CPU占比</li>
<li>%MEM：物理内存占比</li>
<li>VSZ：虚拟内存占用大小KB</li>
<li>RSS：物理内存占用大小KB</li>
<li>TTY：在哪个TTY中运行，tty1-tty7是本地终端，pts/0-255是虚拟终端，?表示由内核直接产生的进程，不需要任何终端</li>
<li>STAT：进程状态
<ul>
<li>D: 无法中断的休眠状态 (通常 IO 的进程)</li>
<li>R: 正在执行中</li>
<li>S: 静止状态</li>
<li>T: 暂停执行</li>
<li>Z: 不存在但暂时无法消除</li>
<li>W: 没有足够的记忆体分页可分配</li>
<li>&lt;: 高优先序的行程</li>
<li>N: 低优先序的行程</li>
<li>L: 有记忆体分页分配并锁在记忆体内 (实时系统或捱A I/O)</li>
</ul></li>
<li>START：运行时间</li>
<li>TIME：当前进程消耗CPU计算时间</li>
<li>COMMAND：进程名称</li>
</ul></li>
<li><p><code>ps le</code>与<code>ps aux</code>一样，但是这个显示的是简写进程</p></li>
<li><p><code>top</code>命令</p>
<p>默认输出</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">top</span> - 01:15:33 up  <span class="token number">1</span>:07,  <span class="token number">1</span> user,  load average: <span class="token number">0.00</span>, <span class="token number">0.01</span>, <span class="token number">0.05</span>
Tasks:  <span class="token number">86</span> total,   <span class="token number">1</span> running,  <span class="token number">85</span> sleeping,   <span class="token number">0</span> stopped,   <span class="token number">0</span> zombie
Cpu<span class="token punctuation">(</span>s<span class="token punctuation">)</span>:  <span class="token number">0.0</span>%us,  <span class="token number">0.3</span>%sy,  <span class="token number">0.0</span>%ni, <span class="token number">99.3</span>%id,  <span class="token number">0.3</span>%wa,  <span class="token number">0.0</span>%hi,  <span class="token number">0.0</span>%si,  <span class="token number">0.0</span>%st
Mem:   1020076k total,   188136k used,   831940k free,    11536k buffers
Swap:  1048572k total,        0k used,  1048572k free,    56896k cached

  PID <span class="token environment constant">USER</span>      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    <span class="token number">7</span> root      <span class="token number">20</span>   <span class="token number">0</span>     <span class="token number">0</span>    <span class="token number">0</span>    <span class="token number">0</span> S  <span class="token number">0.3</span>  <span class="token number">0.0</span>   <span class="token number">0</span>:02.01 events/0
 <span class="token number">1774</span> root      <span class="token number">20</span>   <span class="token number">0</span> <span class="token number">15032</span> <span class="token number">1204</span>  <span class="token number">936</span> R  <span class="token number">0.3</span>  <span class="token number">0.1</span>   <span class="token number">0</span>:00.01 <span class="token function">top</span>
    <span class="token number">1</span> root      <span class="token number">20</span>   <span class="token number">0</span> <span class="token number">19352</span> <span class="token number">1544</span> <span class="token number">1232</span> S  <span class="token number">0.0</span>  <span class="token number">0.2</span>   <span class="token number">0</span>:02.42 init
    <span class="token number">2</span> root      <span class="token number">20</span>   <span class="token number">0</span>     <span class="token number">0</span>    <span class="token number">0</span>    <span class="token number">0</span> S  <span class="token number">0.0</span>  <span class="token number">0.0</span>   <span class="token number">0</span>:00.00 kthreadd
    <span class="token number">3</span> root      RT   <span class="token number">0</span>     <span class="token number">0</span>    <span class="token number">0</span>    <span class="token number">0</span> S  <span class="token number">0.0</span>  <span class="token number">0.0</span>   <span class="token number">0</span>:00.00 migration/0</code></pre>
<p>分别是：</p>
<ul>
<li>系统时间</li>
<li>登录时间</li>
<li>登录用户个数</li>
<li><strong>在之前1,5,15分钟的平均负载，一般不得超过核心数</strong></li>
<li>系统中总进程数</li>
<li>正在运行的进程</li>
<li>睡眠的进程</li>
<li>正在停止的进程</li>
<li>僵尸进程数目</li>
<li>用户占用CPU百分比</li>
<li>系统占用CPU百分比</li>
<li>修改过优先级的用户进程占CPU</li>
<li><strong>空闲CPU百分比</strong></li>
<li>等待IO的进程占用百分比</li>
<li>硬中断请求服务CPU占比</li>
<li>软中断请求服务CPU占比</li>
<li>虚拟时间百分比，虚拟CPU等待实际CPU百分比</li>
<li>内存大小</li>
<li>使用的内存</li>
<li><strong>空闲的内存</strong></li>
<li>缓冲的内存</li>
<li>swap大小</li>
<li>使用的swap</li>
<li>空闲的swap</li>
<li>缓冲的swap</li>
</ul>
<p><strong>top模式下的</strong>交互选项</p>
<ul>
<li><code>?/h</code>显示帮助</li>
<li><code>P</code>按照CPU占有率排序</li>
<li><code>M</code>按照内存使用占比排序</li>
<li><code>N</code>按照PID排序</li>
<li><code>T</code>按照CPU累计运算时间排序</li>
<li><code>k</code>按照pid号给予信号，一般用于关闭进程</li>
<li><code>r</code>按照PID重设优先级</li>
<li><code>q</code>退出</li>
</ul>
<p><strong>选项</strong></p>
<ul>
<li><code>-p PID</code>查看某个PID进程</li>
<li><code>-b</code> 将文件保存</li>
<li><code>-n 1</code>刷新一次，于是<code>top -b -n 1 &gt;&gt; res</code>就是写入进程到文件</li>
</ul></li>
<li><p><code>pstree</code>命令</p>
<p>显示进程树</p>
<p><code>-p</code>显示进程PID</p>
<p><code>-u</code>显示进程所述用户</p></li>
</ul></li>
<li><p>杀死进程</p>
<p>首先需要了解主要的系统信号</p>
<table>
<colgroup>
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 77%" />
</colgroup>
<thead>
<tr class="header">
<th>信号代号</th>
<th>信号名称</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>SIGHUP</td>
<td>关闭进程，重读配置文件重启</td>
</tr>
<tr class="even">
<td>2</td>
<td>SIGINT</td>
<td>终止前台，相当于Ctrl+C</td>
</tr>
<tr class="odd">
<td>8</td>
<td>SIGFPE</td>
<td>发展致命运算错误，例如除0</td>
</tr>
<tr class="even">
<td>9</td>
<td>SIGKILL</td>
<td>立即强制不得阻塞或忽略的结束程序</td>
</tr>
<tr class="odd">
<td>14</td>
<td>SIGALRM</td>
<td>时钟定时型号，计算实际的时间或者时钟时间，alarm函数使用</td>
</tr>
<tr class="even">
<td>15</td>
<td>SIGTERM</td>
<td>正常结束进程的信号</td>
</tr>
<tr class="odd">
<td>18</td>
<td>SIGCONT</td>
<td>让暂停的进程恢复运行，不可阻断</td>
</tr>
<tr class="even">
<td>19</td>
<td>SIGSTOP</td>
<td>暂停前台进程，不可阻断</td>
</tr>
</tbody>
</table>
<ul>
<li><p><code>kill</code>命令</p>
<p><code>kill -信号 PID</code>例如<code>kill -9 12345</code></p></li>
<li><p><code>killall</code> 命令</p>
<p><code>killall -信号 进程名</code>例如<code>kill -9 httpd</code></p>
<ul>
<li><code>-i</code>交互式询问是否杀死</li>
<li><code>-I</code>忽略大小写</li>
</ul></li>
<li><p><code>pkill</code>命令</p>
<ul>
<li><p><code>pkill -信号 进程</code>关闭进程</p></li>
<li><p><code>pkill -信号 -t 终端</code>踢出登录这个终端的用户，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@bogon ~<span class="token punctuation">]</span><span class="token comment"># w</span>
 02:06:49 up  <span class="token number">1</span>:58,  <span class="token number">2</span> users,  load average: <span class="token number">0.04</span>, <span class="token number">0.04</span>, <span class="token number">0.05</span>
<span class="token environment constant">USER</span>     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    <span class="token number">10.0</span>.2.2         00:24    <span class="token number">0</span>.00s  <span class="token number">0</span>.04s  <span class="token number">0</span>.00s w
liukairu pts/1    <span class="token number">10.0</span>.2.2         02:05    <span class="token number">1</span>:23   <span class="token number">0</span>.02s  <span class="token number">0</span>.02s -bash
<span class="token punctuation">[</span>root@bogon ~<span class="token punctuation">]</span><span class="token comment"># pkill -9 -t pts/1</span>
<span class="token punctuation">[</span>root@bogon ~<span class="token punctuation">]</span><span class="token comment"># w</span>
 02:06:57 up  <span class="token number">1</span>:58,  <span class="token number">1</span> user,  load average: <span class="token number">0.04</span>, <span class="token number">0.04</span>, <span class="token number">0.05</span>
<span class="token environment constant">USER</span>     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    <span class="token number">10.0</span>.2.2         00:24    <span class="token number">0</span>.00s  <span class="token number">0</span>.04s  <span class="token number">0</span>.00s w</code></pre></li>
</ul></li>
</ul></li>
</ul>
<h3 id="工作管理">工作管理</h3>
<ul>
<li><p>工作管理简介</p>
<ul>
<li>前台是指当前可以操控和执行命令的这个操作环境，后台是指工作可以自行运行，但是不能直接用ctrl+c来终止它，只能使用fg/bg来调用工作;</li>
<li>当前的登录终端，
只能管理当前终端的工作，而不能管理其他登录终端的工作。如tty1
登录的终端是不能管理tty2终端中的工作的;</li>
<li>放入后台
的命令必须可以持续运行一-段时间，这样我们才能扑捉和操作这个工作。如果把ls命令放入后台执行，它很快就会执行完成，我们很难操作它。</li>
<li>放入后台执行的命令不能和前台用户有交互或需要前台输入，否则放入后台只能暂停，而不能执行。比如vi命令放入后台只能暂停，而不能执行，因为vi需要前台输入信息。top
命令也不能放入后台执行，而只能放入后台暂停，因为top命令需要和前台有交互。</li>
</ul></li>
<li><p>将命令放入后台</p>
<ul>
<li><code>命令 &amp;</code>，只需要将命令+空格+&amp;即可</li>
<li>执行命令的时候Ctrl+Z即可，注意这个会暂停进程</li>
</ul></li>
<li><p><code>jobs</code>查看所有的后台进程，<code>-l</code>查看PID</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@bogon ~<span class="token punctuation">]</span><span class="token comment"># jobs</span>
<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>-  Stopped                 <span class="token function">vi</span>
<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>+  Stopped                 <span class="token function">vim</span>
<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span>   Exit <span class="token number">127</span>                tops</code></pre>
<p>第一列是工作号，是放入后台的顺序，之后显示状态</p></li>
<li><p><code>bg</code>命令：让已经暂停的命令在后台继续执行</p>
<p><code>bg %工作号</code>，这个%可以省略</p></li>
<li><p><code>fg</code>命令：让后台的工作在前台执行</p>
<p><code>fg %工作号</code>，这个%可以省略</p></li>
<li><p>让程序长期后台脱离终端执行</p>
<ul>
<li><p>添加到<code>/etc/rc.local</code></p></li>
<li><p>使用定时任务</p></li>
<li><p>使用<code>nohup</code>命令</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">nohup</span> 需要长期执行的命令 <span class="token operator">&amp;</span></code></pre></li>
</ul></li>
</ul>
<h3 id="系统资源查看">系统资源查看</h3>
<ul>
<li><p><code>vmstat [刷新延时] [刷新次数]</code>命令</p>
<p>例如<code>vmstat 2 3</code>就是每两秒刷新一次，一共刷新三次</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@bogon ~<span class="token punctuation">]</span><span class="token comment"># vmstat 2 4</span>
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   <span class="token function">free</span>   buff  cache   si   so    bi    bo   <span class="token keyword">in</span>   cs us sy <span class="token function">id</span> wa st
 <span class="token number">0</span>  <span class="token number">0</span>      <span class="token number">0</span> <span class="token number">822476</span>  <span class="token number">13412</span>  <span class="token number">60888</span>    <span class="token number">0</span>    <span class="token number">0</span>     <span class="token number">9</span>     <span class="token number">1</span>   <span class="token number">10</span>   <span class="token number">10</span>  <span class="token number">0</span>  <span class="token number">0</span> <span class="token number">100</span>  <span class="token number">0</span>  <span class="token number">0</span>
 <span class="token number">0</span>  <span class="token number">0</span>      <span class="token number">0</span> <span class="token number">822460</span>  <span class="token number">13412</span>  <span class="token number">60892</span>    <span class="token number">0</span>    <span class="token number">0</span>     <span class="token number">0</span>     <span class="token number">0</span>    <span class="token number">8</span>    <span class="token number">9</span>  <span class="token number">0</span>  <span class="token number">0</span> <span class="token number">100</span>  <span class="token number">0</span>  <span class="token number">0</span>
 <span class="token number">0</span>  <span class="token number">0</span>      <span class="token number">0</span> <span class="token number">822460</span>  <span class="token number">13412</span>  <span class="token number">60892</span>    <span class="token number">0</span>    <span class="token number">0</span>     <span class="token number">0</span>     <span class="token number">0</span>    <span class="token number">7</span>    <span class="token number">5</span>  <span class="token number">0</span>  <span class="token number">0</span> <span class="token number">100</span>  <span class="token number">0</span>  <span class="token number">0</span>
 <span class="token number">0</span>  <span class="token number">0</span>      <span class="token number">0</span> <span class="token number">822460</span>  <span class="token number">13412</span>  <span class="token number">60892</span>    <span class="token number">0</span>    <span class="token number">0</span>     <span class="token number">0</span>     <span class="token number">0</span>    <span class="token number">7</span>    <span class="token number">5</span>  <span class="token number">0</span>  <span class="token number">0</span> <span class="token number">100</span>  <span class="token number">0</span>  <span class="token number">0</span></code></pre>
<p>其中：</p>
<ul>
<li><p>procs</p>
<ul>
<li><code>r</code>：等待运行的进程数，越大越忙</li>
<li><code>b</code>：不可唤醒的进程数，越大越忙</li>
</ul></li>
<li><p>memory</p>
<ul>
<li><code>swpd</code>：虚拟内存使用 KB</li>
<li><code>free</code>：空闲内存 KB</li>
<li><code>buff</code>：缓冲内存
KB，buffer(缓冲)是为了提高内存和硬盘或其他I/0设备之间的数据交换的速度而设计的</li>
<li><code>chche</code>：缓存内存
KB，cache(缓存)是为了提高cpu和内存之间的数据交换速度而设计的</li>
</ul></li>
<li><p>Swap：</p>
<ul>
<li><p>si: 每秒从交换区写到内存的大小</p></li>
<li><p>so: 每秒写入交换区的内存大小，越大性能越差</p></li>
</ul></li>
<li><p>IO：（现在的Linux版本块的大小为1024bytes）</p>
<ul>
<li><p>bi: 每秒读取的块数</p></li>
<li><p>bo: 每秒写入的块数，越大越忙</p></li>
</ul></li>
<li><p>系统：</p>
<ul>
<li><p>in: 每秒中断数，包括时钟中断</p></li>
<li><p>cs: 每秒上下文切换数，越大越忙</p></li>
</ul></li>
<li><p>CPU（以百分比表示）：</p>
<ul>
<li>us: 用户进程执行时间(user time)</li>
<li>sy: 系统进程执行时间(system time)</li>
<li>id: 空闲时间(包括IO等待时间),中央处理器的空闲时间
。以百分比表示。</li>
<li>wa: 等待IO时间</li>
<li>st: 被虚拟机盗用的CPU比例</li>
</ul></li>
</ul></li>
<li><p><code>dmesg</code>命令</p>
<p>可以显示开始时内核检测信息，使用grep过滤</p></li>
<li><p><code>free -h</code>空闲内存</p>
<pre class="language-none"><code class="language-none">[root@bogon ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          996M       193M       802M       200K        13M        59M
			总内存		使用		空闲			共享		缓冲		缓存
-&#x2F;+ buffers&#x2F;cache:       120M       875M
			不计缓存缓冲的使用与空闲 
Swap:         1.0G         0B       1.0G</code></pre></li>
<li><p>查看CPU信息：<code>vim /proc/cpuinfo</code></p></li>
<li><p>查看内存信息：<code>vim /proc/meminfo</code></p></li>
<li><p>查看本机登录信息：<code>w</code>或<code>who</code></p></li>
<li><p>查看时间，登录时间，用户，负载(就是tops第一行)：<code>uptime</code></p></li>
<li><p><code>uname</code>查询内核相关</p>
<ul>
<li><code>-a</code>显示所有相关信息</li>
<li><code>-r</code>显示内核版本</li>
<li><code>-s</code>显示内核名称</li>
</ul></li>
<li><p>判断系统位数：<code>uname -a</code>有显示，也可以用<code>file /bin/ls</code>(任意系统命令均可)，也可以用<code>lsb_release -a</code></p></li>
</ul>
<h3 id="系统定时任务">系统定时任务</h3>
<ul>
<li><p>at一次性执行的定时任务</p>
<p>这种定时任务只能执行一次</p>
<p>at执行任务需要at的守护程序atd，与yum的系统独立服务是一样的启动方式</p>
<p>at有黑名单和白名单的机制，白名单在/etc/at.allow，黑名单在/etc.at.deny，规则是</p>
<ul>
<li><p>如果有白名单，只有写入白名单的用户有写入权利</p></li>
<li><p>如果没有白名单只有黑名单那么黑名单用户无法写入，其他可以</p></li>
<li><p>如果两个都不存在，只有root可以使用</p></li>
<li><p>白名单的权限比黑名单高</p></li>
<li><p>一个用户名一行写入即可</p></li>
</ul>
<pre class="language-bash" data-language="bash"><code class="language-bash">at <span class="token punctuation">[</span>选项<span class="token punctuation">]</span> 时间 <span class="token operator">&lt;</span>回车<span class="token operator">></span>
命令
Ctrl+D保存</code></pre>
<ul>
<li>选项
<ul>
<li><code>-m</code>：在at完成后，不论是否都命令有输出都会使用email通知执行at的用户</li>
<li><code>-c 工作号</code>：显示at工作的实际内容</li>
</ul></li>
<li>时间
<ul>
<li>HH:AM</li>
<li>HH:MM YYYY-MM-DD</li>
<li>HH:MM[am|pm] [Month] [date]</li>
<li>HH:MM[am|pm] + number [minutes|hours|days|weeks]</li>
</ul></li>
</ul>
<p>可以使用<code>atq</code>查询工作号</p>
<p><code>atrm 工作号</code>可以删除一个任务</p></li>
<li><p><code>crontab</code>循环执行定时任务</p>
<p>也需要像独立的yum命令一样进行启动和自启动，名字是crond</p>
<p>与at的黑白名单机制一样，位置在<code>/etc/cron.allow</code>与<code>/etc/cron.deny</code></p>
<p>选项</p>
<ul>
<li><code>-e</code> 编辑crontab定时任务</li>
<li><code>-l</code> 查询crontab任务</li>
<li><code>-r</code>
删除用户的所有crontab任务，如果有多个，想要删除一个的话只能<code>-e</code></li>
<li><code>-u 用户名</code>
删除或修改其他用户的crontab任务，只有root可用</li>
</ul>
<p>配置方式是：</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">MM HH DD MM DY 任务</code></pre>
<p>分别是一小时的第几分钟(0-59)，第几小时(0-23)，一个月第几天(1-31)，一年第几个月(1-12)，一周的周几(0-7，0和7都是日)，需要的地方写数字，不需要的地方写*，例如</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token number">10</span> * * * * 命令</code></pre>
<p>相当于<strong>每小时10分</strong>的时候运行</p>
<ul>
<li><p>需要每隔多长时间运行可以写<code>*/时间</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">*/10 * * * * 命令</code></pre>
<p>每隔10min运行</p></li>
<li><p>需要连续不相同的时间执行可以用<code>,</code>隔开</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token number">2,3</span>,4,5 * * * * 命令</code></pre>
<p>每小时2,3,4,5运行</p></li>
<li><p>连续时间范围</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token number">0</span> <span class="token number">5</span> <span class="token number">1</span>-5 * * 命令</code></pre>
<p>相当于每个月1-5号早上五点执行</p></li>
<li><p>星期和日期最好不要写在一起，这个是或的关系</p></li>
<li><p>重启这些服务最好管理员介入，否则恐怕起不来</p></li>
<li><p>定时任务一定要使用绝对路径</p></li>
<li><p>可以<code>* * * * * 用户名 命令</code>指定运行者</p></li>
<li><p>系统也在使用cron，系统的配置文件在<code>/etc/crontab</code>系统将自己准备执行的命令都放在了<code>/etc/cron.daily</code>，cron.weekly......，这都是文件夹，里面的shell就会每天每周...执行</p></li>
</ul></li>
<li><p>anacron</p>
<p>corn固然好用，但是如果发生宕机，或者该运行的时候电脑关机了，anacron可以在开机的时候检查是都有被错过的命令</p>
<p>在<code>/var/spool/anacron</code>中保存着<code>cron.daily|weekly...</code>都保存着anacron上次执行的时间</p>
<p>配置文件在<code>/etc/anacrontab</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable"><span class="token environment constant">SHELL</span></span><span class="token operator">=</span>/bin/sh
<span class="token assign-left variable"><span class="token environment constant">PATH</span></span><span class="token operator">=</span>/sbin:/bin:/usr/sbin:/usr/bin
<span class="token assign-left variable">MAILTO</span><span class="token operator">=</span>root
<span class="token assign-left variable">RANDOM_DELAY</span><span class="token operator">=</span><span class="token number">45</span>			<span class="token comment"># 最大随机延迟</span>
<span class="token assign-left variable">START_HOURS_RANGE</span><span class="token operator">=</span><span class="token number">3</span>-22	<span class="token comment"># 执行的时间范围是0300-2200</span>

<span class="token comment">#period in days   delay in minutes   job-identifier   command</span>
<span class="token number">1</span>       <span class="token number">5</span>       cron.daily              <span class="token function">nice</span> run-parts /etc/cron.daily
<span class="token number">7</span>       <span class="token number">25</span>      cron.weekly             <span class="token function">nice</span> run-parts /etc/cron.weekly
@monthly <span class="token number">45</span>     cron.monthly            <span class="token function">nice</span> run-parts /etc/cron.monthly</code></pre></li>
</ul>
<h2 id="日志管理">日志管理</h2>
<h3 id="日志">日志</h3>
<p>备份的时候一定要保存日志，日志保存半年以上，日志服务是</p>
<table>
<colgroup>
<col style="width: 22%" />
<col style="width: 77%" />
</colgroup>
<thead>
<tr class="header">
<th>日志文件</th>
<th>说 明</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>/var/log/cron</td>
<td>记录与系统定时任务相关的曰志</td>
</tr>
<tr class="even">
<td>/var/log/cups/</td>
<td>记录打印信息的曰志</td>
</tr>
<tr class="odd">
<td>/var/log/dmesg</td>
<td>记录了系统在开机时内核自检的信总。也可以使用dmesg命令直接查看内核自检信息</td>
</tr>
<tr class="even">
<td>/var/log/btmp</td>
<td>记录错误登陆的日志。这个文件是二进制文件，不能直接用Vi查看，而要使用lastb命令查看。命令如下：
[root@localhost log]#lastb root tty1 Tue Jun 4 22:38 - 22:38 (00:00)
#有人在6月4 日 22:38便用root用户在本地终端 1 登陆错误</td>
</tr>
<tr class="odd">
<td>/var/log/lasllog</td>
<td>记录系统中所有用户最后一次的登录时间的曰志。这个文件也是二进制文件.不能直接用Vi
查看。而要使用lastlog命令查看</td>
</tr>
<tr class="even">
<td>/var/Iog/mailog</td>
<td>记录邮件信息的曰志</td>
</tr>
<tr class="odd">
<td>/var/log/messages</td>
<td>它是核心系统日志文件，其中包含了系统启动时的引导信息，以及系统运行时的其他状态消息。I/O
错误、网络错误和其他系统错误都会记录到此文件中。其他信息，比如某个人的身份切换为
root，已经用户自定义安装软件的日志，也会在这里列出。</td>
</tr>
<tr class="even">
<td>/var/log/secure</td>
<td>记录验证和授权方面的倍息，只要涉及账户和密码的程序都会记录，比如系统的登录、ssh的登录、su切换用户，sudo授权，甚至添加用户和修改用户密码都会记录在这个日志文件中</td>
</tr>
<tr class="odd">
<td>/var/log/wtmp</td>
<td>永久记录所有用户的登陆、注销信息，同时记录系统的后动、重启、关机事件。同样，这个文件也是二进制文件.不能直接用Vi查看，而要使用last命令查看</td>
</tr>
<tr class="even">
<td>/var/tun/ulmp</td>
<td>记录当前已经登录的用户的信息。这个文件会随着用户的登录和注销而不断变化，只记录当前登录用户的信息。同样，这个文件不能直接用Vi查看，而要使用w、who、users等命令查看</td>
</tr>
</tbody>
</table>
<p>除系统默认的日志之外，采用 RPM
包方式安装的系统服务也会默认把日志记录在 /var/log/
目录中（源码包安装的服务日志存放在源码包指定的目录中）。不过这些日志不是由
rsyslogd
服务来记录和管理的，而是各个服务使用自己的日志管理文档来记录自身的日志。以下介绍的日志目录在你的
Linux 上不一定存在，只有安装了相应的服务，日志才会出现。服务日志如表 2
所示。</p>
<table>
<thead>
<tr class="header">
<th>日志文件</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>/var/log/httpd/</td>
<td>RPM包安装的apache取务的默认日志目录</td>
</tr>
<tr class="even">
<td>/var/log/mail/</td>
<td>RPM包安装的邮件服务的额外日志因录</td>
</tr>
<tr class="odd">
<td>/var/log/samba/</td>
<td>RPM色安装的Samba服务的日志目录</td>
</tr>
<tr class="even">
<td>/var/log/sssd/</td>
<td>守护进程安全服务目录</td>
</tr>
</tbody>
</table>
<h3 id="日志服务">日志服务</h3>
<p>Linux有rsyslog日志服务，配置文件在<code>/etc/rsyslog.d</code>，里面有诸如此类的内容</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">authpriv.*                  /var/log/secure
<span class="token comment">#日志.要被记录的等级链接符号	位置</span></code></pre>
<p>日志服务连接日志等级的格式如下：</p>
<p>日志服务[连接符号]日志等级 日志记录位置</p>
<p>在这里，连接符号可以被识别为以下三种。</p>
<ol type="1">
<li>“.”代表只要比后面的等级高的（包含该等级）日志都记录。比如，“cron.info”代表cron服务产生的日志，只要日志等级大于等于info级别，就记录。</li>
<li>“.=”代表只记录所需等级的日志，其他等级的日志都不记录。比如，“*.=emerg”代表人和日志服务产生的日志，只要等级是emerg等级，就记录。这种用法极少见，了解就好。</li>
<li>“.！”代表不等于，也就是除该等级的日志外，其他等级的日志都记录。</li>
</ol>
<table>
<colgroup>
<col style="width: 25%" />
<col style="width: 75%" />
</colgroup>
<thead>
<tr class="header">
<th>等级名称</th>
<th>说 明</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>debug (LOG_DEBUG)</td>
<td>一般的调试信息说明</td>
</tr>
<tr class="even">
<td>info (LOG_INFO)</td>
<td>基本的通知信息</td>
</tr>
<tr class="odd">
<td>nolice (LOG_NOTICE)</td>
<td>普通信息，但是有一定的重要性</td>
</tr>
<tr class="even">
<td>warning(LOG_WARNING)</td>
<td>警吿信息，但是还不会影响到服务或系统的运行</td>
</tr>
<tr class="odd">
<td>err(LOG_ERR)</td>
<td>错误信息, 一般达到err等级的信息已经可以影响到服务成系统的运行了</td>
</tr>
<tr class="even">
<td>crit (LOG_CRIT)</td>
<td>临界状况信思，比err等级还要严®</td>
</tr>
<tr class="odd">
<td>alert (LOG_ALERT)</td>
<td>状态信息，比crit等级还要严重，必须立即采取行动</td>
</tr>
<tr class="even">
<td>emerg (LOG_EMERG)</td>
<td>疼痛等级信息，系统已经无法使用了</td>
</tr>
<tr class="odd">
<td>*</td>
<td>代表所有日志等级。比如，“authpriv.*”代表amhpriv认证信息服务产生的日志，所有的日志等级都记录</td>
</tr>
</tbody>
</table>
<p>等级debug-&gt;emerg从低到高</p>
<h3 id="日志轮替">日志轮替</h3>
<p>系统提供了将日志按天分割的功能，但是分开的日志越积越多于是有了轮替功能，可以保留指定天数的日志，RPM包可以被系统识别自动切割，可以修改配置文件支持自己创建的日志，配置文件在<code>/etc/logrotate.conf</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 以下定义的日志每周轮替一次(后面服务没有指定的时候才生效)</span>
weekly
<span class="token comment"># 文件轮替四次</span>
rotate <span class="token number">4</span>
<span class="token comment"># 轮替的时候新建新日志</span>
create
<span class="token comment"># 文件名命名的时候加上日期</span>
dateext
<span class="token comment"># 压缩配置</span>
<span class="token comment">#compress</span>
<span class="token comment"># 加载其他的日志配置文件</span>
include /etc/logrotate.d
<span class="token comment"># 轮替信息</span>
/var/log/wtmp <span class="token punctuation">&#123;</span>
    monthly					<span class="token comment"># 每个月轮替一次&#123;daily|weekly|monthly&#125;</span>
    create 0664 root utmp	 <span class="token comment"># 创建新的日志和权限</span>
	minsize 1M				<span class="token comment"># 如果小于1M即使超过限制也不轮替</span>
    rotate <span class="token number">1</span>				<span class="token comment"># 轮替一次</span>
    <span class="token comment"># mail adress			# 生成是时候发邮件</span>
    <span class="token comment"># missingok				# 日志不存在忽略而不警告</span>
    <span class="token comment"># notifempty			# 空日志不轮替</span>
    <span class="token comment"># size 100k			   # 当文件大于100k自动轮替，而不是按照时间</span>
    sharedscripts			<span class="token comment"># 想要写一些生成日志前后要运行命令需要先写这个</span>
    postrotate				<span class="token comment"># 日志轮替结束后执行以下脚本，想要轮提前执行可以使用命令prerotate</span>
    	<span class="token function">service</span> network restart		<span class="token comment"># 要执行的命令</span>
    	<span class="token comment"># 这里一般是重启服务器服务，尝试使用平滑重启</span>
    endscript
<span class="token punctuation">&#125;</span></code></pre>
<hr />
<p><strong>完结撒花</strong></p>

    </div>

    
    
    
      


    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="Liu Kairui 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.png" alt="Liu Kairui 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Liu Kairui
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://liukairui.me/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" title="Linux系统管理教程笔记">http://liukairui.me/article/Linux系统管理教程笔记/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/%E8%BF%90%E7%BB%B4/" rel="tag"><i class="fa fa-tag"></i> 运维</a>
              <a href="/tags/Linux/" rel="tag"><i class="fa fa-tag"></i> Linux</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/article/Scss%E7%AC%94%E8%AE%B0/" rel="prev" title="Scss笔记">
                  <i class="fa fa-chevron-left"></i> Scss笔记
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/article/Linux%E7%BD%91%E7%BB%9C%E6%9C%8D%E5%8A%A1%E4%B8%8E%E6%95%B0%E6%8D%AE%E5%BA%93%E7%AC%94%E8%AE%B0/" rel="next" title="Linux网络服务与数据库笔记">
                  Linux网络服务与数据库笔记 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="valine-comments"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2019 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Liu Kairui</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>
  <div class="addthis_inline_share_toolbox">
    <script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-6231eb6f709fc868" async="async"></script>
  </div><script
  async
  src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"
></script>

 
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment-precise-range-plugin@1.3.0/moment-precise-range.min.js"></script>
<script>
  function timer() {
    var ages = moment.preciseDiff(moment(),moment(20201101,"YYYYMMDD"));
    ages = ages.replace(/years?/, "年");
    ages = ages.replace(/months?/, "月");
    ages = ages.replace(/days?/, "天");
    ages = ages.replace(/hours?/, "小时");
    ages = ages.replace(/minutes?/, "分");
    ages = ages.replace(/seconds?/, "秒");
    ages = ages.replace(/\d+/g, '<span class="daysCnt" style="color:#1890ff">$&</span>');
    div.innerHTML = `我已在此等候你 ${ages}`;
    div.className="workDays";
  }
  var div = document.createElement("div");
  //插入到copyright之后
  var copyright = document.querySelector(".copyright");
  document.querySelector(".footer-inner").insertBefore(div, copyright.nextSibling);
  timer();
  setInterval("timer()",1000)
</script>



    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@next-theme/pjax@0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdn.jsdelivr.net/npm/hexo-generator-searchdb@1.4.0/dist/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>


  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":"forest","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.14.0/mermaid.min.js","integrity":"sha256-7wT34TI0pEBeEFoi4z+vhuSddGh6vUTMWdqJ2SDe2jg="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>

  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>

  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


  <script class="next-config" data-name="leancloud_visitors" type="application/json">{"enable":true,"app_id":"ABKlVtS4cyaWYEwunPyK3sXt-9Nh9j0Va","app_key":"xxGXdTTEGEVifs2TLB35844I","server_url":"https://abklvts4.lc-cn-e1-shared.com","security":false}</script>
  <script src="/js/third-party/statistics/lean-analytics.js"></script>


  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","mhchem":true,"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.0/es5/tex-mml-chtml.min.js"}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.2.0/quicklink.umd.js" integrity="sha256-4kQf9z5ntdQrzsBC3YSHnEz02Z9C1UeW/E9OgnvlzSY=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":false,"archive":false,"delay":true,"timeout":3000,"priority":true,"url":"http://liukairui.me/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>

<script>
var options = {
  bottom: '64px',
  right: 'unset',
  left: '32px',
  time: '0.5s',
  mixColor: 'transparent',
  backgroundColor: 'transparent',
  buttonColorDark: '#100f2c',
  buttonColorLight: '#fff',
  saveInCookies: true,
  label: '🌓',
  autoMatchOsTheme: true
}
const darkmode = new Darkmode(options);
window.darkmode = darkmode;
darkmode.showWidget();
</script>


<script class="next-config" data-name="valine" type="application/json">{"enable":true,"appId":"ABKlVtS4cyaWYEwunPyK3sXt-9Nh9j0Va","appKey":"xxGXdTTEGEVifs2TLB35844I","serverURLs":"https://abklvts4.lc-cn-e1-shared.com","placeholder":"请开始你的表演","avatar":"identicon","meta":["nick","mail","link"],"pageSize":10,"lang":"zh-CN","visitor":false,"comment_count":true,"recordIP":true,"enableQQ":true,"requiredFields":[],"el":"#valine-comments","path":"/article/Linux%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/"}</script>
<script>
document.addEventListener('page:loaded', () => {
  NexT.utils.loadComments(CONFIG.valine.el)
    .then(() => NexT.utils.getScript(
      'https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js',
      { condition: window.Valine }
    ))
    .then(() => {
      new Valine(CONFIG.valine);
    });
});
</script>


  <script async src="/js/fireworks.js"></script>




  <script src="/js/activate-power-mode.min.js"></script>
  <script>
    POWERMODE.colorful = true;
    POWERMODE.shake = false;
    document.body.addEventListener('input', POWERMODE);
  </script>




  <script src="/js/wobblewindow.js"></script>
  <script>
    //只在桌面版网页启用特效 记得同步到header.njk
    if( window.innerWidth > 768  ){
      $(document).ready(function () {
        
          $('body>main>header').wobbleWindow({
            radius: 50,
            movementTop: false,
            movementLeft: false,
            movementRight: false,
            debug: false,
          });
        

        //
        //  $('body header>aside').wobbleWindow({
        //    radius: 50,
        //    movementLeft: false,
        //    movementTop: false,
        //    movementBottom: false,
        //    position: 'fixed',
        //    debug: false,
        //  });
        //

        
          $('body>footer').wobbleWindow({
            radius: 50,
            movementBottom: false,
            movementLeft: false,
            movementRight: false,
            debug: false,
          });
        
      });
    }
  </script>


 
<script>
  $(document).ready(function(){
    var beian = document.querySelector(".footer-inner > .beian > a");
    var cpr=document.querySelector(".footer-inner > .copyright");
    var cnz=document.querySelector(".workDays");//.parentNode;
    var bsz=document.querySelector(".busuanzi-count");
    if(cnz!=null&&bsz!=null)
      cnz.parentNode.insertBefore(bsz,cnz);
    if(bsz == null || bsz == undefined || document.querySelector("#busuanzi_value_site_pv").innerText=="" || document.querySelector("#busuanzi_value_site_uv").innerText == "")
      bsz.remove();
    checkIndex();
    if(beian){cpr.classList.add('split-line');cpr.appendChild(beian);}
  })
  $(document).on('pjax:complete',checkIndex);
</script>



<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"react":{"opacity":0.7}});</script></body>
</html>
